简体   繁体   English

如何设计一个带有菜单项的树形菜单?

[英]how design a tree menu with an array of items in PHP?


I have an array like below (in PHP): 我有一个下面的数组(在PHP中):

$menu = array(0 => array('id'=>1, 'name'=>'a', 'parent_id'=>''), 1 => array('id'=>2, 'name'=>'b', 'parent_id'=>''), 2 => array('id'=>3, 'name'=>'c', 'parent_id'=>'1'), 3 => array('id'=>4, 'name'=>'d', 'parent_id'=>''), 4 => array('id'=>5, 'name'=>'e', 'parent_id'=>''), 5 => array('id'=>6, 'name'=>'f', 'parent_id'=>'4'));

then I want to echo this array to something like this: 然后我想将此数组回显为这样的东西:

a
|-- c
b
d
|-- f
e

and if I push new item to array like this: array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6')); 如果我像这样将新项目推送到数组: array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6'));

it should be something like this: 应该是这样的:

a
|-- c
b
d
|-- f
|-- |-- g
e

do you have any solution for this? 您对此有什么解决方案吗?
thanks a lot 非常感谢
Reza 雷扎



Hi again, I post my solution but there is something wrong. 再次嗨,我发布了解决方案,但是出了点问题。 if I add something like this array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6')); 如果我添加类似这个array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6')); to my array, it will build above tree without any problem. 到我的数组,它将在树之上构建而没有任何问题。
but if I add this item to array array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'4')); 但是如果我将此项目添加到array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'4'));数组中array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'4')); it will hang because of infinity loop. 它会因无限循环而挂起。
I don't know why it will not exit from the "while" loop. 我不知道为什么它不会从“ while”循环中退出。 can you please review my code and tell me what is wrong? 能否请您检查我的代码并告诉我什么地方不对?
thanks again. 再次感谢。
Reza 雷扎

Try this one out, I guess it's pretty messy... 试试这个,我想这很杂乱...

$menu = array(0 => array('id'=>1, 'name'=>'a', 'parent_id'=>''), 1 => array('id'=>2, 'name'=>'b', 'parent_id'=>''), 2 => array('id'=>3, 'name'=>'c', 'parent_id'=>'1'), 3 => array('id'=>4, 'name'=>'d', 'parent_id'=>''), 4 => array('id'=>5, 'name'=>'e', 'parent_id'=>''), 5 => array('id'=>6, 'name'=>'f', 'parent_id'=>'4'));
array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6'));

$menu_arranged = array();



foreach ($menu as $k=>$v) {         // change array layout
    $id = $v['id'];
    unset($v['id']);
    $menu_arranged[$id] = $v;

}


$i = 0;
while($i < 5) {     // i put 5 as number of subtree levels since i can't find easy way to calculate it
    foreach ($menu_arranged as $k=>$v) {
        $parent = $v['parent_id'];
        if (!empty($parent)) {
            $menu_arranged[$parent][$k] = $menu_arranged[$k];
        }
    }
    $i++;
}

foreach ($menu_arranged as $k => $v) {
    if (!empty($v['parent_id'])) {
        unset($menu_arranged[$k]);
    }
}

echo "<pre>";
print_r($menu_arranged);
echo "</pre>";
    $menu = array(0 => array('id'=>1, 'name'=>'a', 'parent_id'=>''), 1 => array('id'=>2, 'name'=>'b', 'parent_id'=>''), 2 => array('id'=>3, 'name'=>'c', 'parent_id'=>'1'), 3 => array('id'=>4, 'name'=>'d', 'parent_id'=>''), 4 => array('id'=>5, 'name'=>'e', 'parent_id'=>''), 5 => array('id'=>6, 'name'=>'f', 'parent_id'=>'4'));
    array_push ($menu, array('id'=>7, 'name'=>'g', 'parent_id'=>'6'));

    public function sort_array_by_levels ($array)
 {
    $new = array();
    $i = 0;
    $sub_level = 1;
    $search_for_next_root = FALSE;
    $temp = array();
    $root_items_holder = array();

    while (count($array) > 0)
    {
        $set_i_to_zero = FALSE;
        //select first item without parent
        if (! $search_for_next_root)
        {
            foreach ($array as $key => $value)
            {
                if ($value['parent_id'] == 0 || $value['parent_id'] === 0 || $value['parent_id'] == '0')
                {
                    array_push($new, $array[$key]);
                    array_push($root_items_holder, $array[$key]);

                    $temp = $array[$key];
                    unset($array[$key]);
                    $array = array_values($array);
                    $search_for_next_root = TRUE;
                    $i = 0;
                    //break to find sub items for this "root item"
                    break;
                }
            }
        }

        //search for sub items
        //if (count($root_items_holder) > 0)
        if ($search_for_next_root)
        {
            //should search for all items in array
            if ($i < count($array))
            {
                if ($array[$i]['parent_id'] == $temp['id'])
                {
                    //next level sub item found
                    for($j=0; $j < $sub_level; $j++)
                    {
                        $array[$i]['name'] = "|-- " . $array[$i]['name'];
                    }

                    array_push($new, $array[$i]);
                    array_push($root_items_holder, $array[$i]);

                    $temp = $array[$i];
                    //set remove sub-item founded and re-index array again
                    unset($array[$i]);
                    $array = array_values($array);
                    //set $i to zero to search from first cell in array
                    $set_i_to_zero = TRUE;
                    $sub_level++;//to add "|-- " as how many as needed to the sub levels
                }
            } else
            {
                //seems that all of array searched and there is no item with last founded item as it's parent,
                //then we should find next "root item" and search for it's sub levels again.
                if (count($root_items_holder) > 0)
                {
                    array_pop($root_items_holder);

                    if (count($root_items_holder) > 0)
                    {
                        $temp = $root_items_holder[(count($root_items_holder)-1)];
                        $sub_level--;
                    } else
                    {
                        $search_for_next_root = FALSE;
                    }

                    $set_i_to_zero = TRUE;
                } else
                {
                    $search_for_next_root = FALSE;
                    $sub_level = 1;
                }
            }
        }

        if ($set_i_to_zero)
        {
            $i = 0;
        } else
        {
            $i++;
        }
    }

    return $new;                                                
}

 $sorted = sort_array_by_levels ($menu);
 echo "<pre>";
 print_r ($sorted);
 echo "</pre>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM