How to form a PHP tree menu? [closed]

拟墨画扇 提交于 2020-01-03 05:18:10

问题


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

回答1:


$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




回答2:


CODE

<?php

$rows = array(
    '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',
);
sort($rows);

$sorted = array();
foreach ($rows as $row) {
    eval('$sorted["' . implode('"]["', explode('/', $row)) . '"] = array();');
}
print_r($sorted);

?>

OUTPUT

Array
(
    [C:] => Array
        (
            [devices] => Array
                (
                    [data1] => Array
                        (
                            [application1] => Array
                                (
                                )
                            [application2] => Array
                                (
                                )
                            [application3] => Array
                                (
                                )
                        )
                    [data2] => Array
                        (
                            [application1] => Array
                                (
                                )
                            [application2] => Array
                                (
                                )
                            [application3] => Array
                                (
                                )
                        )
                )
        )
)


来源:https://stackoverflow.com/questions/8279935/how-to-form-a-php-tree-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!