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
like:
if(any_key_has_value($my_array)){
//run my query
}
If you define "has value" as "value == 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
}
?>
-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.