Fordnox
Fordnox

Reputation: 23

Unix tree convert to recursive php array

I have a response from remote server like this:

/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

this is the output for command

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

I would like to parse this string to recursive php array or object, something like this. I would show only part of result to get the idea.

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

Response from server can change according to scanned directory. Can someone help me write this function.

Please note that this is response from remote server and php functions can not scan any remote dir.

Upvotes: 2

Views: 369

Answers (1)

Artefacto
Artefacto

Reputation: 97825

$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);

output:

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
                                                        (
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Upvotes: 2

Related Questions