让我的循环递归

时间:2010-12-13 04:13:42

标签: php arrays recursion

我无法理解这一点。我有一个像这样的阵列:

Array
(
    [0] => Array
        (
            [content_id] => 23
            [content_level] => 1
            [content_left] => 2
            [content_right] => 3
            [content_type] => link
            [content_url] => 
            [content_name] => home
            [content_link] => a:3:{s:3:"url";s:7:"_link_/";s:6:"target";s:5:"_self";s:5:"title";s:4:"home";}
            [date_modified] => 2010-07-16 10:29:44
            [content_path] => 
        )

    [1] => Array
        (
            [content_id] => 13
            [content_level] => 1
            [content_left] => 12
            [content_right] => 23
            [content_type] => page
            [content_url] => company
            [content_name] => company
            [content_link] => 
            [date_modified] => 2010-08-13 12:55:44
            [content_path] => company
        )

    [2] => Array
        (
            [content_id] => 25
            [content_level] => 2
            [content_left] => 13
            [content_right] => 14
            [content_type] => page
            [content_url] => About-Us
            [content_name] => About Us
            [content_link] => 
            [date_modified] => 2010-08-13 11:13:40
            [content_path] => company/About-Us
        )

    [3] => Array
        (
            [content_id] => 28
            [content_level] => 2
            [content_left] => 15
            [content_right] => 16
            [content_type] => page
            [content_url] => Tecnic-Quality
            [content_name] => Tecnic Quality
            [content_link] => 
            [date_modified] => 2010-08-13 12:40:23
            [content_path] => company/Tecnic-Quality
        )

    [4] => Array
        (
            [content_id] => 29
            [content_level] => 2
            [content_left] => 17
            [content_right] => 18
            [content_type] => page
            [content_url] => Why-Choose-Tecnic
            [content_name] => Why Choose Tecnic
            [content_link] => 
            [date_modified] => 2010-08-13 12:45:56
            [content_path] => company/Why-Choose-Tecnic
        )

    [5] => Array
        (
            [content_id] => 30
            [content_level] => 2
            [content_left] => 19
            [content_right] => 20
            [content_type] => page
            [content_url] => Tecnic-Functionality
            [content_name] => Tecnic Functionality
            [content_link] => 
            [date_modified] => 2010-08-13 12:50:06
            [content_path] => company/Tecnic-Functionality
        )

    [6] => Array
        (
            [content_id] => 31
            [content_level] => 2
            [content_left] => 21
            [content_right] => 22
            [content_type] => page
            [content_url] => Customer-Focus
            [content_name] => Customer Focus
            [content_link] => 
            [date_modified] => 2010-08-13 12:55:44
            [content_path] => company/Customer-Focus
        )

    [7] => Array
        (
            [content_id] => 16
            [content_level] => 1
            [content_left] => 24
            [content_right] => 59
            [content_type] => module
            [content_url] => products
            [content_name] => products
            [content_link] => 
            [date_modified] => 2010-08-13 14:19:44
            [content_path] => products
        )

    [8] => Array
        (
            [content_id] => 17
            [content_level] => 2
            [content_left] => 25
            [content_right] => 32
            [content_type] => module
            [content_url] => SkyMax-Series
            [content_name] => SkyMax Series
            [content_link] => 
            [date_modified] => 2010-08-13 13:24:18
            [content_path] => products/SkyMax-Series
        )

    [9] => Array
        (
            [content_id] => 0
            [content_name] => SkyMax Patio Retractable Roof System
            [content_path] => products/SkyMax-Series/SkyMax-Patio-Retractable-Roof-System-1281674658
            [content_item] => 1
            [content_level] => 3
            [content_left] => 25
            [content_right] => 32
            [content_type] => page
            [date_modified] => 2010-08-13 15:14:36
        )

    [10] => Array
        (
            [content_id] => 0
            [content_name] => SkyMax Fleetwood Retractable Roof system
            [content_path] => products/SkyMax-Series/skyMax-fleetwood-Retractable-Roof-system
            [content_item] => 1
            [content_level] => 3
            [content_left] => 25
            [content_right] => 32
            [content_type] => page
            [date_modified] => 2010-08-13 15:14:36
        )

我需要遍历它并根据content_path将其转换为更漂亮的东西。我希望它是一个多维数组,元素“item”包含content_id,name,path等以及与其他菜单项相关的其余元素。

这是我的代码:

foreach($menu as $key => $item)
        {
            $parts = explode('/',$item['content_path']);
            switch (count($parts))
            {
            case 3:
                $ul[$parts[0]][$parts[1]][$parts[2]]['item'] = $item;
                break;
            case 2:
                $ul[$parts[0]][$parts[1]]['item'] = $item;
                break;
            case 1:
                $ul[$parts[0]]['item'] = $item;
                break;
            }
        }

工作正常,输出

    Array
(
    [] => Array
        (
            [item] => Array
                (
                    [content_id] => 23
                    [content_level] => 1
                    [content_left] => 2
                    [content_right] => 3
                    [content_type] => link
                    [content_url] => 
                    [content_name] => home
                    [content_link] => a:3:{s:3:"url";s:7:"_link_/";s:6:"target";s:5:"_self";s:5:"title";s:4:"home";}
                    [date_modified] => 2010-07-16 10:29:44
                    [content_path] => 
                )

        )

    [company] => Array
        (
            [item] => Array
                (
                    [content_id] => 13
                    [content_level] => 1
                    [content_left] => 12
                    [content_right] => 23
                    [content_type] => page
                    [content_url] => company
                    [content_name] => company
                    [content_link] => 
                    [date_modified] => 2010-08-13 12:55:44
                    [content_path] => company
                )

            [About-Us] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 25
                            [content_level] => 2
                            [content_left] => 13
                            [content_right] => 14
                            [content_type] => page
                            [content_url] => About-Us
                            [content_name] => About Us
                            [content_link] => 
                            [date_modified] => 2010-08-13 11:13:40
                            [content_path] => company/About-Us
                        )

                )

            [Tecnic-Quality] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 28
                            [content_level] => 2
                            [content_left] => 15
                            [content_right] => 16
                            [content_type] => page
                            [content_url] => Tecnic-Quality
                            [content_name] => Tecnic Quality
                            [content_link] => 
                            [date_modified] => 2010-08-13 12:40:23
                            [content_path] => company/Tecnic-Quality
                        )

                )

            [Why-Choose-Tecnic] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 29
                            [content_level] => 2
                            [content_left] => 17
                            [content_right] => 18
                            [content_type] => page
                            [content_url] => Why-Choose-Tecnic
                            [content_name] => Why Choose Tecnic
                            [content_link] => 
                            [date_modified] => 2010-08-13 12:45:56
                            [content_path] => company/Why-Choose-Tecnic
                        )

                )

            [Tecnic-Functionality] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 30
                            [content_level] => 2
                            [content_left] => 19
                            [content_right] => 20
                            [content_type] => page
                            [content_url] => Tecnic-Functionality
                            [content_name] => Tecnic Functionality
                            [content_link] => 
                            [date_modified] => 2010-08-13 12:50:06
                            [content_path] => company/Tecnic-Functionality
                        )

                )

            [Customer-Focus] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 31
                            [content_level] => 2
                            [content_left] => 21
                            [content_right] => 22
                            [content_type] => page
                            [content_url] => Customer-Focus
                            [content_name] => Customer Focus
                            [content_link] => 
                            [date_modified] => 2010-08-13 12:55:44
                            [content_path] => company/Customer-Focus
                        )

                )

        )

    [products] => Array
        (
            [item] => Array
                (
                    [content_id] => 16
                    [content_level] => 1
                    [content_left] => 24
                    [content_right] => 59
                    [content_type] => module
                    [content_url] => products
                    [content_name] => products
                    [content_link] => 
                    [date_modified] => 2010-08-13 14:19:44
                    [content_path] => products
                )

            [SkyMax-Series] => Array
                (
                    [item] => Array
                        (
                            [content_id] => 17
                            [content_level] => 2
                            [content_left] => 25
                            [content_right] => 32
                            [content_type] => module
                            [content_url] => SkyMax-Series
                            [content_name] => SkyMax Series
                            [content_link] => 
                            [date_modified] => 2010-08-13 13:24:18
                            [content_path] => products/SkyMax-Series
                        )

                    [SkyMax-Patio-Retractable-Roof-System-1281674658] => Array
                        (
                            [item] => Array
                                (
                                    [content_id] => 0
                                    [content_name] => SkyMax Patio Retractable Roof System
                                    [content_path] => products/SkyMax-Series/SkyMax-Patio-Retractable-Roof-System-1281674658
                                    [content_item] => 1
                                    [content_level] => 3
                                    [content_left] => 25
                                    [content_right] => 32
                                    [content_type] => page
                                    [date_modified] => 2010-08-13 15:14:36
                                )

                        )

但由于显而易见的原因,它不会超过3个级别。任何人都有一些关于使这个递归的想法?也许使用一个自称的函数?

1 个答案:

答案 0 :(得分:2)

不需要递归函数,只需for循环即可:

$parts = explode('/',$item['content_path']);
$array = &$ul;
for ($i = 0; $i < count($parts); $i++)
  $array = &$array[$parts[$i]];
$array['item'] = $item;

这会循环遍历$parts,每次迭代都会获得对$ul的下一个维度的引用。