[英]How to filter the value of the key in PHP?
I have a array that contain different keys and i want to filter the key based on the value TRUE OR FALSE. 我有一个包含不同键的数组,我想根据值TRUE或FALSE筛选键。 I am in trouble please help. 我有麻烦请帮忙。
Here is my code 这是我的代码
foreach ($combine as $data) {
unset($data['user_name'], $data['date']);
if (array_values($data) == TRUE) {
pr(array_keys($data));
}
}
Here is the array 这是数组
Array
(
[microsoft] => FALSE
[health_care] => TRUE
[nasa_cerification_type_i] => TRUE
[nasa_cerification_type_ii] => TRUE
[nasa_cerification_type_iii] => TRUE
)
I think this is what you want to do: 我认为这是您想要做的:
foreach($combine as $data){
unset($data['user_name'], $data['date']); //we don't need these
$valid = [];
foreach($data as $n => $v){
if($v === true){ //be careful! Are the values really boolean? then use ===, otherwise use ==
$valid[] = $n;
}
}
//do something with $valid
print_r($valid); //etc..
}
$trueArray=array_filter($array, function ($ar){
return ($ar==true);
});
$falseArray=array_filter($array, function ($ar){
return ($ar==false);
});
print_r($trueArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.