Unix ağaç özyinelemeli php dizi dönüştürmek

1 Cevap php

Ben bu gibi uzak sunucudan bir yanıt var:

/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

Bu komutun çıkışı

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

Sunucu yanıtı taranan dizine göre değiştirebilirsiniz. Birisi bana bu işlevi yazmak yardımcı olabilir.

Bu fonksiyonlar herhangi bir uzak dir tarayamazsınız uzak sunucu ve php den tepki olduğunu unutmayınız.

1 Cevap

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

çıktı:

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

                                                )

                                        )

                                )

                        )

                )

        )

)