phparraysvalidationmultidimensional-array

How can I check if a value exists in multiple arrays where keys are the same


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.


Solution

  • $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.