Reputation: 2659
I got an array that looks like this:
[0] => Array (
[status] => success
[field] => voornaam
)
[1] => Array (
[status] => success
[field] => achternaam
)
[2] => Array (
[status] => success
[field] => telefoon
)
[3] => Array (
[status] => error
[field] => email
[message] => Vul een email in
)
[4] => Array (
[status] => success
[field] => huisnummer
)
[5] => Array (
[status] => success
[field] => postcode
)
[6] => Array (
[status] => success
[field] => straat
)
[7] => Array (
[status] => success
[field] => woonplaats
)
I want to check if error
is present in any of those arrays. How can I do that?
This is how the array is built:
if (empty($accountform['voornaam'])) {
$return[] = array('status' => 'error', 'field' => 'voornaam', 'message' => 'Vul een voornaam in');
} else {
$return[] = array('status' => 'success', 'field' => 'voornaam');
}
if (empty($accountform['achternaam'])) {
$return[] = array('status' => 'error', 'field' => 'achternaam', 'message' => 'Vul een achternaam in');
} else {
$return[] = array('status' => 'success', 'field' => 'achternaam');
}
if (empty($accountform['telefoon'])) {
$return[] = array('status' => 'error', 'field' => 'telefoon', 'message' => 'Vul een telefoonnummer in');
} else {
$return[] = array('status' => 'success', 'field' => 'telefoon');
}
if (empty($accountform['email'])) {
$return[] = array('status' => 'error', 'field' => 'email', 'message' => 'Vul een email in');
}else if(!filter_var($accountform['email'], FILTER_VALIDATE_EMAIL)){
$return[] = array('status' => 'error', 'field' => 'email', 'message' => 'Vul een geldig emailadres in');
} else {
$return[] = array('status' => 'success', 'field' => 'email');
}
if (empty($accountform['huisnummer'])) {
$return[] = array('status' => 'error', 'field' => 'huisnummer', 'message' => 'Vul een huisnummer in');
} else {
$return[] = array('status' => 'success', 'field' => 'huisnummer');
}
if (empty($accountform['postcode'])) {
$return[] = array('status' => 'error', 'field' => 'postcode', 'message' => 'Vul een postcode in');
} else {
$return[] = array('status' => 'success', 'field' => 'postcode');
}
if (empty($accountform['straat'])) {
$return[] = array('status' => 'error', 'field' => 'straat', 'message' => 'Vul een straat in');
} else {
$return[] = array('status' => 'success', 'field' => 'straat');
}
if (empty($accountform['woonplaats'])) {
$return[] = array('status' => 'error', 'field' => 'woonplaats', 'message' => 'Vul een woonplaats in');
} else {
$return[] = array('status' => 'success', 'field' => 'woonplaats');
}
I tried:
if (in_array('error', $return)) {
echo "There is an error";
}
But the if statement never fires.
Upvotes: 1
Views: 396
Reputation: 86
Why your code is not working is because $result has values inside another arrays like [0] and [1] and so on like this ..
Array
(
[0] => Array
(
[status] => error
[field] => telefoon
[message] => Vul een telefoonnummer in
)
[1] => Array
(
[status] => success
[field] => achternaam
)
)
So if you want your code to work you'll need $result[0], $result[1] and so on.. Hence your code should be inside foreach loop
foreach ($return as $key => $value) {
if (in_array('error', $value)) {
echo "There is an error";
}
}
$value now has $result datas without [0][1], now you can compare with in_array fn.
Upvotes: 0
Reputation: 1795
you can use array_search and check if it exists or not
$key = array_search('error', array_column($return, 'status'));
if($key){
echo 'found';
}else{
echo 'not found';
}
Upvotes: 0
Reputation: 17805
$status_rows = array_column($your_array,'status');
var_dump(in_array('error',$status_rows));
You can use array_column to fetch all statuses and use in_array to check it's existence.
Upvotes: 2