循环遍历多维数组并取消设置匹配项

时间:2017-10-24 09:43:19

标签: php arrays multidimensional-array unset

我有一个多维数组,我想遍历每个级别的每个项目并删除“children”和“attribute_id”都为空的任何项目(在这种情况下,最后2个项目的键为56和70)

Array
(
    [11] => Array
        (
            [page_id] => 11
            [page_parent_id] => 0
            [attribute_id] => 8
            [children] => Array
                (
                )

        )

    [12] => Array
        (
            [page_id] => 12
            [page_parent_id] => 0
            [attribute_id] => 
            [children] => Array
                (
                    [49] => Array
                        (
                            [page_id] => 49
                            [page_parent_id] => 12
                            [attribute_id] => 
                            [children] => Array
                                (
                                    [50] => Array
                                        (
                                            [page_id] => 50
                                            [page_parent_id] => 49
                                            [attribute_id] => 32
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [13] => Array
        (
            [page_id] => 13
            [page_parent_id] => 0
            [attribute_id] => 9
            [children] => Array
                (
                )

        )

    [46] => Array
        (
            [page_id] => 46
            [page_parent_id] => 0
            [attribute_id] => 
            [children] => Array
                (
                    [52] => Array
                        (
                            [page_id] => 52
                            [page_parent_id] => 46
                            [attribute_id] => 34
                            [children] => Array
                                (
                                )

                        )

                    [53] => Array
                        (
                            [page_id] => 53
                            [page_parent_id] => 46
                            [attribute_id] => 
                            [children] => Array
                                (
                                    [54] => Array
                                        (
                                            [page_id] => 54
                                            [page_parent_id] => 53
                                            [attribute_id] => 35
                                            [children] => Array
                                                (
                                                )

                                        )
                                    [70] => Array
                                        (
                                            [page_id] => 70
                                            [page_parent_id] => 53
                                            [attribute_id] => 
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [56] => Array
                        (
                            [page_id] => 56
                            [page_parent_id] => 46
                            [attribute_id] => 
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

到目前为止,我已经得到了这个,我可以回应我想要取消的项目,但我不知道如何获得它,所以我实际上可以解开它。

function loop($a) {
    if(empty($a['children']))
    {
        if(empty($a['attribute_id']))
        {
            var_dump($a);
        }
    } else {
        foreach($a['children'] as $v) {
            loop($v);
        }
    }
}

foreach($pages as $p)
{
    loop($p);
}

1 个答案:

答案 0 :(得分:1)

这样的事情:

function loop($a) {
    $newArray = array();
    foreach ($a as $k => $v) {
        if (isset($v['children']) && count($v['children']) > 0 ) {
            $v['children'] = loop($v['children']);
        }

        if ((isset($v['children']) && count($v['children']) > 0) || !empty($v['attribute_id'])) {
            $newArray[$k] = $v;
        }
    }
    return $newArray;
}
$newArray = loop($a);