如何形成一个PHP树菜单? [英] How to form a PHP tree menu?

查看:181
本文介绍了如何形成一个PHP树菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些路径存储在我的数据库中作为:它们是未排序的,可以是任何长度。



--------路径---- ----


C:/ devices / data1



< devices / data1 / application2



C:/ devices / data1 / application1



< application3



C:/ devices / data2 / application2



C:/ devices / data2 / application1



C:/ devices / data1 / application3


  C>装置> data1> application1 
application2
application3
data2> application1
application2
application3

您能帮我解决这个问题吗? / p>

希望快速回复。非常感谢!



Ulvund的解决方案适用于我,但现在我需要生成指向href标签的链接:



我尝试了以下操作:



http:/ /www.pbalan01.0fees.net/god1.php



请帮助我删除额外的节点:它当前生成...

 
C:/ devices / data1
data1 / application1
application2
application3
data2
data2 / application1
application2
application3


解决方案

  $ paths [] =C:/ devices / data1; 
$ paths [] =C:/ devices / data1 / application2;
$ paths [] =C:/ devices / data1 / application1;
$ paths [] =C:/ devices / data2 / application3;
$ paths [] =C:/ devices / data2 / application2;
$ paths [] =C:/ devices / data2 / application1;
$ paths [] =C:/ devices / data1 / application3;


函数to_tree(& $ multifruit,$ fruit){
if(count($ fruit)> 2){
$ shifted = array_shift水果);
to_tree($ multifruit [$ shifted],$ fruit);
return $ multifruit;
} else {
return $ multifruit [$ fruit [0]] [] = $ fruit [1];
}
}

sort($ paths);
foreach($ paths as $ path){
$ path = explode(/,$ path);
to_tree($ multifruit,$ path);
}

print_r($ multifruit);
/ *结果
数组

[C:] => Array

[devices]
[0] => data1
[data1] => Array

[0] => application1
[1] => application2
[2] => application3


[data2] => Array

[0] => application1
[1] => application2
[2] => application3









* /

>:



要获得完整的期望结果(或最接近我们的帮助):使用以下代码: http://hpaste.org/54546



查看 http://redditlist.com/test2.php



编辑3



http:// redditlist。 com / test3.php



http:// hpaste .org / 54599


I have some paths stored in my db as: They are unsorted and can be any length.

--------Paths--------

C:/devices/data1

C:/devices/data1/application2

C:/devices/data1/application1

C:/devices/data2/application3

C:/devices/data2/application2

C:/devices/data2/application1

C:/devices/data1/application3

I need to form a menu as below:

C > devices > data1> application1 
                     application2 
                     application3 
              data2> application1 
                     application2 
                     application3 

Could you please help me solve this problem?

Wish for some quick responses. Thanks!

Ulvund's solution works for me but now I need to generate the links to the href tags:

I tried the following:

http://www.pbalan01.0fees.net/god1.php

Please help me remove the extra node: it currently generates...

C:/devices/data1
           data1/application1
                 application2
                 application3
           data2
           data2/application1
                 application2
                 application3

解决方案

$paths[] = "C:/devices/data1";
$paths[] = "C:/devices/data1/application2";
$paths[] = "C:/devices/data1/application1";
$paths[] = "C:/devices/data2/application3";
$paths[] = "C:/devices/data2/application2";
$paths[] = "C:/devices/data2/application1";
$paths[] = "C:/devices/data1/application3";


function to_tree(&$multifruit, $fruit) {
    if (count($fruit)>2) {
        $shifted = array_shift($fruit);
        to_tree($multifruit[$shifted], $fruit);
        return $multifruit;
    } else {
        return $multifruit[$fruit[0]][] = $fruit[1];
    }   
}   

sort($paths);
foreach($paths as $path) {
   $path = explode("/",$path);
   to_tree($multifruit, $path);
}   

print_r($multifruit);
/* Result
Array
(
    [C:] => Array
        (
            [devices] => Array
                (
                    [0] => data1
                    [data1] => Array
                        (
                            [0] => application1
                            [1] => application2
                            [2] => application3
                        )

                    [data2] => Array
                        (
                            [0] => application1
                            [1] => application2
                            [2] => application3
                        )

                )

        )

)


*/

EDIT:

To have your full desired result (or the closest you will get with my help): Use the following code: http://hpaste.org/54546

See a sample on http://redditlist.com/test2.php

EDIT 3:

http://redditlist.com/test3.php

http://hpaste.org/54599

这篇关于如何形成一个PHP树菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆