Unix树转换为递归php数组

时间:2010-05-14 16:18:26

标签: php unix tree recursion

我有来自远程服务器的回复:

/home/computer/Downloads
|-- /home/computer/Downloads/Apple
|   `-- /home/computer/Downloads/Apple/Pad
|-- /home/computer/Downloads/Empty_Folder
`-- /home/computer/Downloads/Subfolder
    |-- /home/computer/Downloads/Subfolder/Empty
    `-- /home/computer/Downloads/Subfolder/SubSubFolder
        `-- /home/computer/Downloads/Subfolder/SubSubFolder/Test

这是命令的输出

computer@athome:$ tree -df --noreport -L 5 /home/computer/Downloads/

我想将这个字符串解析为递归的php数组或对象,就像这样。 我只会展示部分结果来获得这个想法。

array(
    'title' => '/home/computer/Downloads',
    'children' => array(
        0 => array(
            'title' => '/home/computer/Downloads/Apple',
            'children' => array( ...
        ) 
    )
);

来自服务器的响应可以根据扫描的目录进行更改。有人可以帮我写这个功能。

请注意,这是远程服务器的响应,php函数无法扫描任何远程目录。

1 个答案:

答案 0 :(得分:2)

$str = <<<EOD
/home/computer/Downloads
|-- /home/computer/Downloads/Apple
|   `-- /home/computer/Downloads/Apple/Pad
|-- /home/computer/Downloads/Empty_Folder
`-- /home/computer/Downloads/Subfolder
    |-- /home/computer/Downloads/Subfolder/Empty
    `-- /home/computer/Downloads/Subfolder/SubSubFolder
        `-- /home/computer/Downloads/Subfolder/SubSubFolder/Test
EOD;

$str = preg_replace('/\r\n?/', "\n", $str); //normalize line endings

$dirs = explode("\n", $str);
var_dump($dirs);
$arr = array(
    "title" => reset($dirs),
    "children" => array(),
);
$lastindent = 0;
$parentstack = array();
$lastelem = &$arr;
//debug_zval_dump(&$arr);
for ($i = 1; $i < count($dirs); $i++) {
    $indent = strpos($dirs[$i], '/') / 4;
    unset($thiselem);
    $thiselem = array(
        "title" => $dirs[$i],
        "children" => array(),
    );

    if ($indent > $lastindent) { //first child
        $parentstack[] = &$lastelem;
        $lastelem["children"][] = &$thiselem;
    }
    elseif ($indent == $lastindent) { //sibling
        $parentstack[count($parentstack)-1]["children"][] = &$thiselem;
    }
    else { //ident < lastindent
        for ($j = $lastindent; $j > $indent; $j--) {
            array_pop($parentstack);
        }
        $parentstack[count($parentstack)-1]["children"][] = &$thiselem;
    }

    $lastindent = $indent;
    $lastelem = &$thiselem;
}

print_r($arr);

输出:

array(8) {
  [0]=>
  string(24) "/home/computer/Downloads"
  [1]=>
  string(34) "|-- /home/computer/Downloads/Apple"
  [2]=>
  string(42) "|   `-- /home/computer/Downloads/Apple/Pad"
  [3]=>
  string(41) "|-- /home/computer/Downloads/Empty_Folder"
  [4]=>
  string(38) "`-- /home/computer/Downloads/Subfolder"
  [5]=>
  string(48) "    |-- /home/computer/Downloads/Subfolder/Empty"
  [6]=>
  string(55) "    `-- /home/computer/Downloads/Subfolder/SubSubFolder"
  [7]=>
  string(64) "        `-- /home/computer/Downloads/Subfolder/SubSubFolder/Test"
}
Array
(
    [title] => /home/computer/Downloads
    [children] => Array
        (
            [0] => Array
                (
                    [title] => |-- /home/computer/Downloads/Apple
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [title] => |   `-- /home/computer/Downloads/Apple/Pad
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [title] => |-- /home/computer/Downloads/Empty_Folder
                    [children] => Array
                        (
                        )

                )

            [2] => Array
                (
                    [title] => `-- /home/computer/Downloads/Subfolder
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [title] =>     |-- /home/computer/Downloads/Subfolder/Empty
                                    [children] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [title] =>     `-- /home/computer/Downloads/Subfolder/SubSubFolder
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [title] =>         `-- /home/computer/Downloads/Subfolder/SubSubFolder/Test
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)