doudeng5218 2017-10-24 09:43
浏览 77
已采纳

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

I have a multidimensional array and I want to loop through every item on every level and remove any items where 'children' and 'attribute_id' are both empty (in this case the last 2 items with a key of 56 and 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
                                (
                                )

                        )

                )

        )

)

I've got this so far which I can echo out the items I want to unset but I'm not sure how to get it so I can actually unset it.

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条回答 默认 最新

  • douwei1128 2017-10-24 10:00
    关注

    Something like this:

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?