2

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.

1 Answer 1

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

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

                                                )

                                        )

                                )

                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.