检查数组中是否只有某些值不为空?
Check if only some values are not empy in array?
我有这样的数组:-
$atts = [
"images_url" => "1",
"link_list" => "2",
];
但有时数组可以是这样的:-
$atts = [
"images_url" => "1"
];
或者首先可以基于空是不是空我需要一些额外的逻辑,如何检查数组的某些值是否不为空或存在并做一些额外的逻辑?
你可以像下面那样做:-
if(count($atts) > count(array_filter($atts))){
echo "some indexes are empty";
}
对于你在评论中的另一个问题
foreach($atts as $key=>$val){
if(!empty($atts[$key])){ // will check both index exist and have some value
echo $val;
}
}
我有这样的数组:-
$atts = [
"images_url" => "1",
"link_list" => "2",
];
但有时数组可以是这样的:-
$atts = [
"images_url" => "1"
];
或者首先可以基于空是不是空我需要一些额外的逻辑,如何检查数组的某些值是否不为空或存在并做一些额外的逻辑?
你可以像下面那样做:-
if(count($atts) > count(array_filter($atts))){
echo "some indexes are empty";
}
对于你在评论中的另一个问题
foreach($atts as $key=>$val){
if(!empty($atts[$key])){ // will check both index exist and have some value
echo $val;
}
}