Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中设计带有项目数组的树菜单?_Php_Arrays_Drop Down Menu_Tree - Fatal编程技术网

如何在PHP中设计带有项目数组的树菜单?

如何在PHP中设计带有项目数组的树菜单?,php,arrays,drop-down-menu,tree,Php,Arrays,Drop Down Menu,Tree,我有一个如下的数组(在PHP中): 然后我想将这个数组回传到如下内容: a |-- c b d |-- f e a |-- c b d |-- f |-- |-- g e 如果我将新项按如下方式推送到数组: array\u push($menu,array('id'=>7,'name'=>g','parent\u id'=>6') 应该是这样的: a |-- c b d |-- f e a |-- c b d |-- f |-- |-- g e 您对此有什么解决方案吗? 非常感谢 雷扎


我有一个如下的数组(在PHP中):

然后我想将这个数组回传到如下内容:

a
|-- c
b
d
|-- f
e
a
|-- c
b
d
|-- f
|-- |-- g
e
如果我将新项按如下方式推送到数组:
array\u push($menu,array('id'=>7,'name'=>g','parent\u id'=>6')

应该是这样的:

a
|-- c
b
d
|-- f
e
a
|-- c
b
d
|-- f
|-- |-- g
e
您对此有什么解决方案吗?
非常感谢
雷扎




您好,我发布了我的解决方案,但出现了一些问题。如果我添加这样的内容
array\u push($menu,array('id'=>7,'name'=>g','parent\u id'=>6')
到我的数组中,它将毫无问题地构建在树的上方。
但是如果我将此项添加到数组中,
数组推送($menu,array('id'=>7,'name'=>g','parent_id'=>4')由于无限循环,它将挂起。
我不知道为什么它不会退出“while”循环。你能检查一下我的代码并告诉我出了什么问题吗?
再次感谢。
雷扎

试试这个,我想它相当混乱

$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\u id'=>''),1=>array('id'=>2,'name'=>b','parent\u id'=>''),2=>array('id'=>3,'name'=>c','parent\u id'=>1'),3=>array('id'=>4,'name'=>d','parent\u id'=>d','parent\u id'=>''),4=>array('id'>5,'name'=>e','parent\u id'=>4'=>;
数组推送($菜单,数组('id'=>7,'name'=>g','parent\u id'=>6');
$menu_排列=数组();
foreach($菜单为$k=>$v){//更改数组布局
$id=$v['id'];
未结算($v['id']);
$menu_排列[$id]=$v;
}
$i=0;
而($i<5){//我把5作为子树级别的数量,因为我找不到简单的方法来计算它
foreach($k=>v){
$parent=$v['parent_id'];
如果(!空($parent)){
$menu_排列[$parent][$k]=$menu_排列[$k];
}
}
$i++;
}
foreach($k=>v){
如果(!空($v['parent_id'])){
未设置($menu_排列[$k]);
}
}
回声“;
打印(已安排菜单);
回声“;
$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','name'>'e','parent'id'=>4'=>parent'id'=>;
数组推送($菜单,数组('id'=>7,'name'=>g','parent\u id'=>6');
公共函数按级别排序数组($array)
{
$new=array();
$i=0;
$sub_level=1;
$search\u for\u next\u root=FALSE;
$temp=array();
$root_items_holder=array();
而(计数($array)>0)
{
$set_i_to_zero=FALSE;
//选择没有父项的第一项
if(!$search\u查找下一个\u根)
{
foreach($key=>$value的数组)
{
如果($value['parent_id']==0 | |$value['parent_id']==0 | |$value['parent_id']==0')
{
数组推送($new,$array[$key]);
数组推送($root\u items\u holder,$array[$key]);
$temp=$array[$key];
未设置($array[$key]);
$array=数组值($array);
$search\u for\u next\u root=TRUE;
$i=0;
//断开以查找此“根项”的子项
打破
}
}
}
//搜索子项
//if(计数($root\u项目\u持有人)>0)
if($search\u for\u next\u root)
{
//应该搜索数组中的所有项
如果($i0)
{
数组\u pop($root\u items\u holder);
if(计数($root\u项目\u持有人)>0)
{
$temp=$root\u items\u holder[(计数($root\u items\u holder)-1)];
$sub_级别--;
}否则
{
$search\u for\u next\u root=FALSE;
}
$set_i_to_zero=真;
}否则
{
$search\u for\u next\u root=FALSE;
$sub_level=1;
}
}
}
如果($set_i_至_零)
{
$i=0;
}否则
{
$i++;
}
}
退回$new;
}
$sorted=按级别对数组排序($menu);
回声“;
打印(已排序);
回声“;

您对阵列结构的想法是什么?我从数据库中获取这些项目,我可以更改一切。但我不知道怎么做!我花了大约16个小时在上面,但当我在列表中添加多个“子级别”时,我的解决方案并没有真正起作用。是的,这一个很棘手-!-我想你真的不需要这么做