简体   繁体   中英

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. 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);

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.

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