循环多维数组并删除某些键

发布于 2024-08-28 12:50:13 字数 6706 浏览 4 评论 0原文

我有一个基于下面数组的嵌套树结构:

Array
(
    [1] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => Startpage
            [uri] => 125
            [basename] => index.php
            [child] => 
        )

    [23] => Array
        (
            [id] => 23
            [parent] => 0
            [name] => Events
            [uri] => 0
            [basename] => 
            [child] => Array
                (
                    [24] => Array
                        (
                            [id] => 24
                            [parent] => 23
                            [name] => Public news
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [27] => Array
                                        (
                                            [id] => 27
                                            [parent] => 24
                                            [name] => Add
                                            [uri] => 100
                                            [basename] => news.public.add.php
                                            [child] => 
                                        )

                                    [28] => Array
                                        (
                                            [id] => 28
                                            [parent] => 24
                                            [name] => Overview
                                            [uri] => 101
                                            [basename] => news.public.overview.php
                                            [child] => 
                                        )

                                )

                        )

                    [25] => Array
                        (
                            [id] => 25
                            [parent] => 23
                            [name] => Private news
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [29] => Array
                                        (
                                            [id] => 29
                                            [parent] => 25
                                            [name] => Add
                                            [uri] => 67
                                            [basename] => news.private.add.php
                                            [child] => 
                                        )

                                    [30] => Array
                                        (
                                            [id] => 30
                                            [parent] => 25
                                            [name] => Overview
                                            [uri] => 68
                                            [basename] => news.private.overview.php
                                            [child] => 
                                        )

                                )

                        )

                    [26] => Array
                        (
                            [id] => 26
                            [parent] => 23
                            [name] => Calendar
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [31] => Array
                                        (
                                            [id] => 31
                                            [parent] => 26
                                            [name] => Add
                                            [uri] => 69
                                            [basename] => news.event.add.php
                                            [child] => 
                                        )

                                    [32] => Array
                                        (
                                            [id] => 32
                                            [parent] => 26
                                            [name] => Overview
                                            [uri] => 70
                                            [basename] => news.event.overview.php
                                            [child] => 
                                        )

                                )

                        )

                )

        )
)

我正在寻找一个函数来循环(递归?)通过数组并删除一些键。

在我的系统中,我可以允许用户访问某些功能/页面,如果我拒绝访问整个“块”“事件”,该数组将如下所示:

array (
  1 => 
  array (
    'id' => '1',
    'parent' => '0',
    'name' => 'Start page',
    'uri' => '125',
    'basename' => 'index.php',
    'child' => '',
  ),
  23 => 
  array (
    'id' => '23',
    'parent' => '0',
    'name' => 'Events',
    'uri' => '0',
    'basename' => NULL,
    'child' => 
    array (
      24 => 
      array (
        'id' => '24',
        'parent' => '23',
        'name' => 'Public news',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
      25 => 
      array (
        'id' => '25',
        'parent' => '23',
        'name' => 'Private news',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
      26 => 
      array (
        'id' => '26',
        'parent' => '23',
        'name' => 'Calendar',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
    ),
  )
)

正如您在上面看到的,整个“块”“事件”是无用的现在,因为没有与每个选项关联的页面。因此,我需要找到“basename”为空且 child 不是数组或数组为空的所有“键”并删除它们。 我在搜索网站时发现了这个函数:

function searchAndDestroy(&$a, $key, $val){
    foreach($a as $k => &$v){
        if(is_array($v)){
            $r = searchAndDestroy($v, $key, $val);
            if($r) {
                unset($a[$k]);
            }
        } elseif ($key == $k && $val == $v) {
            return true;
        }
    }
    return false;
}

它可用于删除数组中任何位置的键,但仅基于一件事,例如删除“parent”等于“23”的所有键。但我需要找到并删除(取消设置)“basename”为空且 child 不是数组或数组为空的所有键。 任何人都可以帮助我并可能调整上面的功能吗?

谢谢你,

I've got a nested tree structure which is based on the array below:

Array
(
    [1] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => Startpage
            [uri] => 125
            [basename] => index.php
            [child] => 
        )

    [23] => Array
        (
            [id] => 23
            [parent] => 0
            [name] => Events
            [uri] => 0
            [basename] => 
            [child] => Array
                (
                    [24] => Array
                        (
                            [id] => 24
                            [parent] => 23
                            [name] => Public news
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [27] => Array
                                        (
                                            [id] => 27
                                            [parent] => 24
                                            [name] => Add
                                            [uri] => 100
                                            [basename] => news.public.add.php
                                            [child] => 
                                        )

                                    [28] => Array
                                        (
                                            [id] => 28
                                            [parent] => 24
                                            [name] => Overview
                                            [uri] => 101
                                            [basename] => news.public.overview.php
                                            [child] => 
                                        )

                                )

                        )

                    [25] => Array
                        (
                            [id] => 25
                            [parent] => 23
                            [name] => Private news
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [29] => Array
                                        (
                                            [id] => 29
                                            [parent] => 25
                                            [name] => Add
                                            [uri] => 67
                                            [basename] => news.private.add.php
                                            [child] => 
                                        )

                                    [30] => Array
                                        (
                                            [id] => 30
                                            [parent] => 25
                                            [name] => Overview
                                            [uri] => 68
                                            [basename] => news.private.overview.php
                                            [child] => 
                                        )

                                )

                        )

                    [26] => Array
                        (
                            [id] => 26
                            [parent] => 23
                            [name] => Calendar
                            [uri] => 0
                            [basename] => 
                            [child] => Array
                                (
                                    [31] => Array
                                        (
                                            [id] => 31
                                            [parent] => 26
                                            [name] => Add
                                            [uri] => 69
                                            [basename] => news.event.add.php
                                            [child] => 
                                        )

                                    [32] => Array
                                        (
                                            [id] => 32
                                            [parent] => 26
                                            [name] => Overview
                                            [uri] => 70
                                            [basename] => news.event.overview.php
                                            [child] => 
                                        )

                                )

                        )

                )

        )
)

I'm looking for a function to loop (recursive?) through the array and remove some keys.

I my system I can allow users to certain functions/pages and if I deny access to the whole "block" "Events", the array will look like this:

array (
  1 => 
  array (
    'id' => '1',
    'parent' => '0',
    'name' => 'Start page',
    'uri' => '125',
    'basename' => 'index.php',
    'child' => '',
  ),
  23 => 
  array (
    'id' => '23',
    'parent' => '0',
    'name' => 'Events',
    'uri' => '0',
    'basename' => NULL,
    'child' => 
    array (
      24 => 
      array (
        'id' => '24',
        'parent' => '23',
        'name' => 'Public news',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
      25 => 
      array (
        'id' => '25',
        'parent' => '23',
        'name' => 'Private news',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
      26 => 
      array (
        'id' => '26',
        'parent' => '23',
        'name' => 'Calendar',
        'uri' => '0',
        'basename' => NULL,
        'child' => '',
      ),
    ),
  )
)

As you can see above, the whole "block" "Events" is useless right now, becuase there is no page associated with each option. So I need to find all "keys" where "basename" is null AND where child is not an array or where the array is empty and remove them.
I found this function when searching the site:

function searchAndDestroy(&$a, $key, $val){
    foreach($a as $k => &$v){
        if(is_array($v)){
            $r = searchAndDestroy($v, $key, $val);
            if($r) {
                unset($a[$k]);
            }
        } elseif ($key == $k && $val == $v) {
            return true;
        }
    }
    return false;
}

It can be used to remove a key any where in the array, but only based in one thing, for example remove all keys where "parent" equals "23". But I need to find and remove (unset) all keys where "basename" is null AND where child isn't an array or where the array is empty.
Can anyone help me out and possibly tweak the function above?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-09-04 12:50:13

不要在搜索函数中测试要销毁哪些元素,而是传递一个函数来测试目标。

function searchAndDestroy(&$a, $targetp){
    foreach($a as $k => &$v){
        if(is_array($v)){
            searchAndDestroy($v, $targetp);
        } 
        if ($targetp($k, $v)) {
            unset($a[$k]);
        }
    }
}

searchAndDestroy($menu, function ($k, $v) {
        return is_array($v) 
            && array_key_exists('basename', $v) && empty($v['basename'])
            && (empty($v['child']) || count($v['child']) == 0);
    });

对于 PHP < 5.3(或者如果您在多个位置调用该函数的 searchAndDestroy),为该函数命名并传递该名称而不是匿名函数。

Rather than putting the test for which elements to destroy in the search function, pass a function to test for targets.

function searchAndDestroy(&$a, $targetp){
    foreach($a as $k => &$v){
        if(is_array($v)){
            searchAndDestroy($v, $targetp);
        } 
        if ($targetp($k, $v)) {
            unset($a[$k]);
        }
    }
}

searchAndDestroy($menu, function ($k, $v) {
        return is_array($v) 
            && array_key_exists('basename', $v) && empty($v['basename'])
            && (empty($v['child']) || count($v['child']) == 0);
    });

For PHP < 5.3 (or if you call searchAndDestroy with that function in more than one spot), name the function and pass the name rather than an anonymous function.

天暗了我发光 2024-09-04 12:50:13

解决了!将此函数添加到我的类中:

private function cleanTree(&$arr){
    foreach($arr as $key => &$item) {
        if(!$item["child"] && empty($item["basename"])){
            unset($arr[$key]);

    }elseif(is_array($item["child"])){
        if(count($item["child"]) == 0){
            unset($arr[$item["id"]]);
        }else{
            $this->cleanTree($item["child"]);
        }
    }
}

}

要删除 ROOT 级别以及其他任何人上不必要的元素,只需运行上述两次即可。

Solved it! Added this function to my class:

private function cleanTree(&$arr){
    foreach($arr as $key => &$item) {
        if(!$item["child"] && empty($item["basename"])){
            unset($arr[$key]);

    }elseif(is_array($item["child"])){
        if(count($item["child"]) == 0){
            unset($arr[$item["id"]]);
        }else{
            $this->cleanTree($item["child"]);
        }
    }
}

}

To remove unnecessary elements on the ROOT level(s) as well as anyone else, just run the above twice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文