PHP splitting array into two arrays based on value

前端 未结 5 1093
小蘑菇
小蘑菇 2021-02-19 06:43

I have a PHP array that I am trying to split into 2 different arrays. I am trying to pull out any values that contain the word \"hidden\". So one array would contain all the v

5条回答
  •  时光取名叫无心
    2021-02-19 07:31

    You can use array_filter:

    function filtreHiddens($e) {
        if (isset($e['hidden']) && $e['hidden']) return true;
        else return false;
    }
    
    function filtreNotHiddens($e) {
        if (isset($e['hidden']) && !$e['hidden']) return true;
        else return false;
    }
    
    $arrayToFiltre = array(
        array('hidden' => true, 'someKey' => 'someVal'),
        array('hidden' => false, 'someKey1' => 'someVal1'),
        array('hidden' => true, 'someKey2' => 'someVal3'),
    );
    
    $hidden = array_filter($arrayToFiltre, 'filtreHiddens');
    $notHidden = array_filter($arrayToFiltre, 'filtreNotHiddens');
    
    print_r($hidden);
    print_r($notHidden);
    

提交回复
热议问题