递归PHP数组

时间:2016-04-12 00:23:50

标签: php arrays recursion

我有以下问题。我想在数组中找到这些值,当不是true或不存在时,删除数组

示例:

$array =array(
            array(
                'menu'  => 'hide',
                'label' => 'Notice',
                'class' => 'menu-icon fa fa-newspaper-o',
                'route' => 'notice',
                'action'=> 'index',
            ),

            array(
                'label' => 'Nivel',
                'class' => 'menu-icon fa fa-file-text-o',
                'uri'   => '#',
                'pages' => array(
                    array(
                        'label' => 'Tur',
                        'pages' => array(
                            array(
                                'menu'   => 'hide',
                                'label'  => 'Content',
                                'route'  => 'g7t',
                                'action' => 'index',
                                'params' => array(
                                    'nivel' => 'gdfa',
                                ),
                            ),
                            array(
                                'menu'   => 'hide',
                                'label'  => 'Plan',
                                'route'  => 'plan',
                                'action' => 'index',
                                'params' => array(
                                    'nivel' => 'gdr',
                                ),
                            ),
                            array(
                                'menu'   => 'hide',
                                'label'  => 'Lan',
                                'action' => 'index',
                                'params' => array(
                                    'nivel' => 'bcn',
                                ),
                                'pages' => array(
                                    array(
                                        'menu'   => 'hide',
                                        'label'  => 'lan2',
                                        'route'  => 'lan2',
                                        'action' => 'list',
                                        'params' => array(
                                            'nivel' => 'gr',
                                        ),
                                    ),
                                ),
                            ),
                            array(
                                'menu'   => 'hide',
                                'label'  => 'notes',
                                'route'  => 'notes',
                                'action' => 'index',
                                'params' => array(
                                    'nivel' => 'gran',
                                ),
                            ),

                        ),
                    ),
                    array(
                        'label' => 'Disp',
                        'route' => '/',
                    ),
                ),
            ),
        );

现在我想把这些位置放在数组中:

   $arrayfilter= [
        'Content'            => true,
        'Disp'               => false,
        'lan2'               => true,
        'Plan'               => false,
        'notes'              => true,
        'Nivel'              => true,
        'Lan'                => true,
    ];

现在就是那样

array(
         array(
            'label' => 'Nivel',
            'class' => 'menu-icon fa fa-file-text-o',
            'uri'   => '#',
            'pages' => array(
                array(
                    'label' => 'Tur',
                    'pages' => array(
                        array(
                            'menu'   => 'hide',
                            'label'  => 'Content',
                            'route'  => 'g7t',
                            'action' => 'index',
                            'params' => array(
                                'nivel' => 'gdfa',
                            ),
                        ),
                        array(
                            'menu'   => 'hide',
                            'label'  => 'Lan',
                            'action' => 'index',
                            'params' => array(
                                'nivel' => 'bcn',
                            ),
                            'pages' => array(
                                array(
                                    'menu'   => 'hide',
                                    'label'  => 'lan2',
                                    'route'  => 'lan2',
                                    'action' => 'list',
                                    'params' => array(
                                        'nivel' => 'gr',
                                    ),
                                ),
                            ),
                        ),
                        array(
                            'menu'   => 'hide',
                            'label'  => 'notes',
                            'route'  => 'notes',
                            'action' => 'index',
                            'params' => array(
                                'nivel' => 'gran',
                            ),
                        ),

                    ),
                ),

            ),
        ));

2 个答案:

答案 0 :(得分:1)

有一些空闲时间,所以我创建了一个循环..

一个不那么干净的解决方案,我不推荐它,但这可以根据你上面的输出工作。

foreach($array as $key1 => $levelOne) {
    echo $key1."<br/>";
    if(is_array($levelOne)) {
        foreach($levelOne as $key2 => $levelTwo) {
            echo "leveloneLoop ".$key2."<br/>";

            if( $key2 == "label" && ((array_key_exists($levelOne['label'],$arrayfilter) && !$arrayfilter[$levelOne['label']])
                || !array_key_exists($levelOne['label'],$arrayfilter)) ) {
                echo "delete array['".$key1."']<br/>";
                unset($array[$key1]);
            }

            // loop to $pages ($pages, $route)                      
            foreach($levelTwo as $key3 => $levelThree) {
                foreach($levelThree as $key4 => $levelFour) {                       
                    echo "levelThreeLoop ".$key4."<br/>";
                    if( $key4 == "label" && ((array_key_exists($levelThree['label'],$arrayfilter) && !$arrayfilter[$levelThree['label']])
                        || !array_key_exists($levelThree['label'],$arrayfilter)) ) {

                        echo "delete array['".$key1."']['".$key2."']['".$key3."']['".$key4."']<br/>";
                        unset($array[$key1][$key2][$key3]);
                    }
                    foreach($levelFour as $key5 => $levelFive) {
                        foreach($levelFive as $key6 => $levelSix) {


                            if( $key6 == "label" && ((array_key_exists($levelFive['label'],$arrayfilter) && !$arrayfilter[$levelFive['label']])
                                || !array_key_exists($levelFive['label'],$arrayfilter)) ) {
                                echo "delete array['".$key1."']['".$key2."']['".$key3."']['".$key4."']['".$key5."']<br/>";
                                unset($array[$key1][$key2][$key3][$key4][$key5]);
                            }
                        }
                    }
                }
            }
        }
    }
}

注意到您还没有在过滤器上添加Tur,所以我添加了它。

$arrayfilter= [
    'Content'            => true, 
    'Disp'               => false, 
    'lan2'               => true, 
    'Plan'               => false, 
    'notes'              => true, 
    'Nivel'              => true,
    'Lan'                => true,
    'Tur'                => true
];

如果你想更深入,那么只需添加一个循环。

答案 1 :(得分:0)

I suggest starting with a function that accepts an Array as its argument, looping through the elements of this array, and recursively calls itself in case its required to go one level deeper in the tree (i.e. you have 'pages' sub-array), passing the element by reference. If its not required to go deeper, just check the 'leaf' node match the filter criteria and unset() it in case it doesn't.