0

このタスクに問題があります。PHPの新機能です。再帰的なfuncruionを使用する必要があると思いますが、その使用方法がわかりません

私はこのような配列を持っています:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [name] => New Folder
        )
    [1] => Array (
            [id] => 2
            [parent_id] => 1
            [name] => New Folder2
        )
    [2] => Array
        (
            [id] => 3
            [parent_id] => 2
            [name] => New Folder3
        )
    ......
    [5] => Array
        (
            [id] => 6
            [parent_id] => 0
            [name] => New Folder6
        )
    [6] => Array (
            [id] => 7
            [parent_id] => 6
            [name] => New Folder7
        )
    [7] => Array
        (
            [id] => 8
            [parent_id] => 6
            [name] => New Folder8
        )
)

私は次のようにディレクトリのパスを構築する必要があります:

/New Folder
/New Folder/New Folder2
/New Folder/New Folder2/New Folder3
...
/New Folder6
/New Folder6/New Folder7
/New Folder6/New Folder8

この仕事を手伝ってくれませんか。どうも。

4

1 に答える 1

0
$txtTree[0]="/";

foreach($treeArray as $branch){

    if(isset($txtTree[$branch['parent_id']])) {
        $txtTree[$branch['id']] = $txtTree[$branch['parent_id']] . $branch['name']. "/";
    }
} 

print_r($txtTree);


Array
(
    [0] => /
    [1] => /New Folder/
    [2] => /New Folder/New Folder2/
    [3] => /New Folder/New Folder2/New Folder3/
    [6] => /New Folder6/
    [7] => /New Folder6/New Folder7/
    [8] => /New Folder6/New Folder8/
)
于 2013-03-04T11:59:42.510 に答える