本文介绍了通过多维数组循环,并删除某些键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这是基于以下阵列的嵌套树结构:

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' => '',
      ),
    ),
  )
)

正如你可以在上面看到,整个块,事件是没用的,现在,监守没有与每个选项相关的页面。所以,我需要找到所有的钥匙,其中基本名称为NULL,并且其中,孩子是不是一个数组或者数组为空,并删除它们。
我发现这个功能搜索网站时:

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

有可用于移除钥匙任何其中阵列中,但只设在一件事,例如除去其中父等于23的键。但我需要找到并删除(未设置)所有的键,其中基本名称为NULL,并且在那里的孩子是不是一个数组或者数组为空。
谁能帮我出并可能调整上述功能?

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?

感谢您,

推荐答案

解决它!添加了这个功能上我的课:

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"]);
        }
    }
}

}

要删除根目录下(S)以及其他任何人不必要的元素,只是执行上面的两倍。

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

这篇关于通过多维数组循环,并删除某些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:37