[英]PHP - check multiple array key, if key has value
I have an array: 我有一个数组:
$my_array = array(1 => 'has value', 2 =>'', 3 => '');
I want to run a if statment to check if all key have value or not, if there is not any value for all keys then return false 我想运行一个if语句来检查所有键是否都具有值,如果所有键都没有任何值,则返回false
like: 喜欢:
if(any_key_has_value($my_array)){
//run my query
}
If you define "has value" as "value == true
": 如果将“具有值”定义为“值
== true
”:
if (count(array_filter($array)) == count($array)) {
echo 'All elements have values';
}
function hasValue($v) {
return strval($v) != '';
}
$res_array = array_filter($my_array, 'hasValue');
// any key has value
$any_key_has_value = 0 < sizeof($res_array);
// all keys have values
$all_keys_have_values = sizeof($my_array) == sizeof($res_array);
$my_array = array(1 => 'has value', 2 =>'', 3 => '');
simply try this 只需尝试这个
if (array_filter($my_array)) {
// here first value `has value`
}else{
// all values are empty
}
Try this: 尝试这个:
<?php
$array = array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null);
if (!array_filter($array)) {
echo "All keys have null values";
}
else
{
// do something
}
?>
- --
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.