繁体   English   中英

如何在PHP中过滤键的值?

[英]How to filter the value of the key in PHP?

我有一个包含不同键的数组,我想根据值TRUE或FALSE筛选键。 我有麻烦请帮忙。

这是我的代码

 foreach ($combine as $data) {
       unset($data['user_name'], $data['date']);
       if (array_values($data) == TRUE) {
            pr(array_keys($data));
       }
  }

这是数组

 Array
    (
        [microsoft] => FALSE
        [health_care] => TRUE
        [nasa_cerification_type_i] => TRUE
        [nasa_cerification_type_ii] => TRUE
        [nasa_cerification_type_iii] => TRUE
    )

我认为这是您想要做的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM