Search code examples
phparraysrecursionfilterarray-filter

How to filter multi-dimensional array (with more than two levels) by value?


Assuming the following three-dimensional array is given:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [foo] => bar
                )

            [1] => Array
                (
                    [foo] => bar2
                )

            [2] => Array
                (
                    [foo] => bar3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [foo] => baz
                )

            [1] => Array
                (
                    [foo] => bar
                )

            [2] => Array
                (
                    [foo] => bar5
                )

        )

)

Now I need to filter it. If [foo] is not equal to bar the corresponding inner array should get removed. The result would be:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [foo] => bar
                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [foo] => bar
                )

        )

)

Can this be achieved with array_filter or array_walk[_recursive]?


Solution

  • Might be more complex than needed but maybe a combination:

    $array = array_map(function($v) {
                       return array_filter($v, function($v) {
                                               return $v['foo'] === 'bar'; }); }, $array);
    

    This should also work:

    foreach($array as &$value) {
        $value = array_filter($value, function($v) { return $v['foo'] === 'bar'; });
    }
    

    You can use a recursive function for deeper or unknown nesting:

    function filterNotSomething(&$array, $key, $val) {
        foreach($array as $k => $v) {            
            if(isset($v[$key]) && $v[$key] !== $val) {
                unset($array[$k]);
            }
            elseif(is_array($v)) {
                filterNotSomething($array[$k], $key, $val);
            }
            if(empty($array[$k])) {
                unset($array[$k]);
            }
        }
    }
    
    filterNotSomething($array, 'foo', 'bar');