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

妖精的绣舞 提交于 2021-02-10 08:34:30

问题


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]?


回答1:


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


来源:https://stackoverflow.com/questions/34051220/how-to-filter-multi-dimensional-array-with-more-than-two-levels-by-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!