簡體   English   中英

遍歷多維數組並取消設置匹配項

[英]Loop through multidimensional array and unset matching items

我有一個多維數組,我想遍歷每個級別上的每個項目並刪除“ children”和“ attribute_id”均為空的所有項目(在本例中,最后兩個項目的鍵值為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);
}

像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM