嵌套数组的

时间:2016-09-24 07:30:15

标签: php arrays loops recursion

我有以下数组,因为数组可以有n个子数,所以无法知道数组的深度。

$menu = [
    [
        'name' => 'home',
        'label' => 'Home',
        'uri' => '/home',
        'order' => 1,
        'attributes' => [
            'class' => ['home-class', 'home-class-2'],
            'id' => ['home-id']
        ]
    ], [
        'name' => 'about',
        'label' => 'About',
        'uri' => '/about',
        'order' => 2,
        'attributes' => [
            'class' => [],
            'id' => []
        ],
        'child' => [
            [
                'name' => 'company_profile',
                'label' => 'Company Profile',
                'uri' => '/company-profile',
                'order' => 1,
                'attributes' => [
                    'class' => [],
                    'id' => []
                ]
            ], [
                'name' => 'team',
                'label' => 'Team',
                'uri' => '/team',
                'order' => 2,
                'attributes' => [
                    'class' => ['team-class', 'team-class-2'],
                    'id' => ['team-id']
                ],
                'child' => [
                    [
                        'name' => 'management_team',
                        'label' => 'Management Team',
                        'uri' => '/management-team',
                        'order' => 1,
                        'attributes' => [
                            'class' => [],
                            'id' => []
                        ]
                    ],
                    [
                        'name' => 'development_team',
                        'label' => 'Development Team',
                        'uri' => '/development-team',
                        'order' => 2,
                        'attributes' => [
                            'class' => [],
                            'id' => []
                        ]
                    ],

                ]
            ],
        ]
    ], [
        'name' => 'services',
        'label' => 'Services',
        'uri' => '/services',
        'order' => 3,
        'attributes' => [
            'class' => [],
            'id' => []
        ],
        'child' => [
            [
                'name' => 'web_application',
                'label' => 'Web Application',
                'uri' => '/web-application',
                'order' => 1,
                'attributes' => [
                    'class' => [],
                    'id' => []
                ]
            ], [
                'name' => 'mobile_application',
                'label' => 'Mobile Application',
                'uri' => '/mobile-application',
                'order' => 2,
                'attributes' => [
                    'class' => [],
                    'id' => []
                ]
            ], [
                'name' => 'cms_development',
                'label' => 'CMS Development',
                'uri' => '/cms-development',
                'order' => 3,
                'attributes' => [
                    'class' => [],
                    'id' => []
                ]
            ],
        ]
    ]
];

我想循环遍历并将数据传递给对象,例如。

$nav = new Navigation\Menu('main');
foreach ($menu as $item) {
    // Parent element
    $navItem = new Navigation\Item($item['name']);
    $navItem->setLabel($item['label']);
    $navItem->setUri($item['uri']);
    $nav->addItem($navItem);
    if (isset($item['child']) && is_array($item['child'])) {
        // First child
        foreach ($item['child'] as $child1) {
            $childItem1 = new Navigation\Item($child1['name']);
            $childItem1->setLabel($child1['label']);
            $childItem1->setUri($child1['uri']);
            $navItem->addChild($childItem1);
            if (isset($child1['child']) && is_array($child1['child'])) {
                // Second child
                foreach ($child1['child'] as $child2) {
                    $childItem2 = new Navigation\Item($child2['name']);
                    $childItem2->setLabel($child2['label']);
                    $childItem2->setUri($child2['uri']);
                    $childItem1->addChild($childItem2);
                }
            }
        }
    }
}

这有效,但有问题。如你所见,我手动循环遍历每个孩子,我不想要这个,我正在寻找的是,它必须递归地迭代数组,允许添加任意数量的任何深度的子。

我尝试了array_walk_recursive或自定义递归函数而没有任何结果。任何解决此问题的指针都表示赞赏。

感谢。

2 个答案:

答案 0 :(得分:1)

这是一个小的递归脚本,如果它是一个数组或一个对象,它应该遍历每一个,每个递归都将返回该对象。现在需要对您的使用进行一些编辑。但它应该给你一个起点。

function Navigation($item) {
    if (is_object($item)) {
        foreach (get_object_vars($item) as $property => $value) {
            //If item is an object, then run recursively
            if (is_array($value) || is_object($value)) {
                $item->$property =  Navigation($item);
            } else {
                $navItem->setLabel($item['label']);
                $navItem->setUri($item['uri']);
                $nav->addItem($navItem);
            }
        }
        return $nav;
    } elseif (is_array($item)) {
        foreach ($item as $property => $value) {
            //If item is an array, then run recursively
            if (is_array($value) || is_object($value)) {
                $item[$property] =  Navigation($item);
            } else {
                $navItem->setLabel($item['label']);
                $navItem->setUri($item['uri']);
                $nav->addItem($navItem);
            }
        }
        return $nav;
    }
    $navItem->setLabel($item['label']);
    $navItem->setUri($item['uri']);
    $nav->addItem($navItem);
}

答案 1 :(得分:1)

终于搞清楚了。

以下是我使用自定义递归函数的方法。

function recursive($menu, &$nav, $child = false, $parent = null)
{
    foreach ($menu as $page) {
        $navItem = new Navigation\Item($page['name']);
        if (false == $child) {
            $nav->addItem($navItem);
        } else {
            $parent->addChild($navItem);
        }
        if (isset($page['child'])) {
            recursive($page['child'], $nav, true, $navItem);
        }
    }
}

$nav = new Navigation\Menu('main');
recursive($menu, $nav);