PHP 5.2: Filter array with multiple arguments performance

前端 未结 1 1223
南方客
南方客 2021-01-15 20:21

I\'m using this code to filter a multidimensional array:

$sourceArray = array(
                array(\'name\'=>\'banana\', \'color\'=>\'green\'),
              


        
相关标签:
1条回答
  • 2021-01-15 20:37

    PHP 5.2 accepts the name of a callback in the array_filter function.

    function filter($arr)
    {
        $arrayToCompare = array('type'=>'fruits','has_name'=>'banana', 'has_color'=>'yelow');
    
        return $arr['name'] == $arrayToCompare['has_name']
              && $arr['color'] == $arrayToCompare['has_color'];
    }
    
    $filtered = array_filter($masterItems, 'filter');
    
    0 讨论(0)
提交回复
热议问题