用于在php中创建菜单子菜单的过滤器数组

时间:2016-04-20 04:35:29

标签: php arrays array-filter

我的现有数组就像这样

$menus = array(
        0 =>array(
                "id"=> 1,
                "name"=> "A",
                "parent_id"=> 0
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "B",
                "parent_id"=> 1
            ),
         2 =>array(
                "id"=> 3,
                "name"=> "C",
                "parent_id"=> 1
            ),
         3 =>array(
                "id"=> 4,
                "name"=> "D",
                "parent_id"=> 2
            ),
        4 =>array(
                "id"=> 5,
                "name"=> "E",
                "parent_id"=> 0
            ),

     );

我想像下面的输出那样创建那个数组。

预期数组

Array
(
    [0] => Array
        (
            [Menu] => Array
                (
                   [0] => Array
                          ( [name] => A
                            [Sub_menu] => Array
                             (
                                  [0] => Array
                                      (
                                        [name] => B
                                      ),
                                  [1] => Array
                                     (
                                        [name] => C
                                        [Sub_menu] => Array
                                          (
                                              [0] => Array
                                              (
                                                [name] => D
                                              ),

                                          )
                                     )
                             )
                          ),
                   [1] => Array
                          ( [name] => E

                          )
                )
         )
)

我尝试了一些代码,但这些代码无效。我的工作输出就像这张图片 enter image description here

在菜单中的foreach循环中再次重复。

2 个答案:

答案 0 :(得分:3)

您可以使用此功能递归构造菜单。

function makeMenu($items, $parentId)
{
    $menu = array_filter($items, function ($item) use ($parentId) {
        return $item['parent_id'] == $parentId;
    });
    foreach ($menu as &$item) {
        $subItems = makeMenu($items, $item['id']);
        if (!empty($subItems)) {
            $item['sub_menu'] = $subItems;
        }
    }
    return $menu;
}

使用$parentId = 0调用它,它会正常工作。

$readyMenu = makeMenu($itemsArray, 0);

答案 1 :(得分:0)

此代码适用于您,请尝试...

$all = array(
        0 =>array(
                "id"=> 1,
                "name"=> "A",
                "parent_id"=> 0
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "B",
                "parent_id"=> 1
            ),
         2 =>array(
                "id"=> 3,
                "name"=> "C",
                "parent_id"=> 1
            ),
         3 =>array(
                "id"=> 4,
                "name"=> "D",
                "parent_id"=> 2
            ),
        4 =>array(
                "id"=> 5,
                "name"=> "E",
                "parent_id"=> 0
            ),
    );  

foreach($all as $key => $val)
{
    if($val['parent_id']==0)
    {
        $data[]=$val;

        foreach($all as $k => $v)
        { 
            if($val['id'] == $v['parent_id']){
                $data[$key]['sub_menu'][]= $v;
                foreach($all as $a => $s)
                { 
                    if($v['id'] == $s['parent_id']){
                        $data[$key]['sub_menu'][$key]['sub_menu'][]= $s;
                    }
                }
            }
        }
    }
}
echo "<pre>"; print_r($data);
?

这将输出如下:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => A
            [parent_id] => 0
            [sub_menu] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => B
                            [parent_id] => 1
                            [sub_menu] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [name] => D
                                            [parent_id] => 2
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [name] => C
                            [parent_id] => 1
                        )

                )

        )

    [1] => Array
        (
            [id] => 5
            [name] => E
            [parent_id] => 0
        )

)