我得到了一个看起来像这样的数组:
[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
)
我想检查error
是否存在于那些数组中。我该怎么办?
这是数组的构建方式:
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');
}
我尝试过:
if (in_array('error', $return)) {
echo "There is an error";
}
但是if语句永远不会触发。
答案 0 :(得分:2)
$status_rows = array_column($your_array,'status');
var_dump(in_array('error',$status_rows));
您可以使用array_column来获取所有状态,并使用in_array来检查它的存在。
答案 1 :(得分:0)
您可以使用array_search并检查其是否存在
$key = array_search('error', array_column($return, 'status'));
if($key){
echo 'found';
}else{
echo 'not found';
}
答案 2 :(得分:0)
您的代码为什么不起作用,是因为$ result在另一个数组中有值,例如[0]和[1]等,如此..
Array
(
[0] => Array
(
[status] => error
[field] => telefoon
[message] => Vul een telefoonnummer in
)
[1] => Array
(
[status] => success
[field] => achternaam
)
)
因此,如果您希望代码能够正常工作,则需要$ result [0],$ result [1]等。因此,您的代码应位于foreach循环内
foreach ($return as $key => $value) {
if (in_array('error', $value)) {
echo "There is an error";
}
}
$ value现在有$ result数据,没有[0] [1],现在可以与in_array fn进行比较。