Как преобразовать простой массив путей к файлам во вложенный массив, представляющий файловую иерархию в PHP?

Мне нужно написать функцию в PHP, которая принимает следующий входной массив:

$inputArray = [
[
"path" => "/C",
"basename" => "C",
"size" => 4096,
"modified" => 1540579748,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Recycle.Bin",
"basename" => "Recycle.Bin",
"size" => 0,
"modified" => 1539012172,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Pictures",
"basename" => "Pictures",
"size" => 3164422144,
"modified" => 1540581569,
"type" => "file",
"contents" => []
],
[
"path" => "/C/Videos",
"basename" => "Videos",
"size" => 970752,
"modified" => 1540579792,
"type" => "file",
"contents" => []
],
[
"path" => "/C/Documents and Settings",
"basename" => "Documents and Settings",
"size" => 4096,
"modified" => 1539022708,
"type" => "link",
"contents" => []
],
[
"path" => "/C/Documents and Settings/Public",
"basename" => "Public",
"size" => 4096,
"modified" => 1539012079,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Documents and Settings/desktop.ini",
"basename" => "desktop.ini",
"size" => 174,
"modified" => 1506692592,
"type" => "file",
"contents" => []
],
[
"path" => "/C/PerfLogs",
"basename" => "PerfLogs",
"size" => 0,
"modified" => 1506692704,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Program Files",
"basename" => "Program Files",
"size" => 4096,
"modified" => 1540579745,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Program Files (x86)",
"basename" => "Program Files (x86)",
"size" => 4096,
"modified" => 1506692707,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/ProgramData",
"basename" => "ProgramData",
"size" => 4096,
"modified" => 1539012665,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Recovery",
"basename" => "Recovery",
"size" => 0,
"modified" => 1539022717,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/System Volume Information",
"basename" => "System Volume Information",
"size" => 4096,
"modified" => 1539012967,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Users",
"basename" => "Users",
"size" => 4096,
"modified" => 1539012384,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Windows",
"basename" => "Windows",
"size" => 24576,
"modified" => 1539011957,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/pagefile.sys",
"basename" => "pagefile.sys",
"size" => 1207959552,
"modified" => 1540581479,
"type" => "file",
"contents" => []
],
[
"path" => "/C/swapfile.sys",
"basename" => "swapfile.sys",
"size" => 268435456,
"modified" => 1540581479,
"type" => "file",
"contents" => []
]
];

и создает следующий выходной массив:

$outputArray = [
[
"path" => "/C",
"basename" => "C",
"size" => 4096,
"modified" => 1540579748,
"type" => "dir",
"contents" => [
[
"path" => "/C/Recycle.Bin",
"basename" => "Recycle.Bin",
"size" => 0,
"modified" => 1539012172,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Pictures",
"basename" => "Pictures",
"size" => 3164422144,
"modified" => 1540581569,
"type" => "file",
"contents" => []
],
[
"path" => "/C/Videos",
"basename" => "Videos",
"size" => 970752,
"modified" => 1540579792,
"type" => "file",
"contents" => []
],
[
"path" => "/C/Documents and Settings",
"basename" => "Documents and Settings",
"size" => 4096,
"modified" => 1539022708,
"type" => "link",
"contents" => [
[
"path" => "/C/Documents and Settings/Public",
"basename" => "Public",
"size" => 4096,
"modified" => 1539012079,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Documents and Settings/desktop.ini",
"basename" => "desktop.ini",
"size" => 174,
"modified" => 1506692592,
"type" => "file",
"contents" => []
]
]
],
[
"path" => "/C/PerfLogs",
"basename" => "PerfLogs",
"size" => 0,
"modified" => 1506692704,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Program Files",
"basename" => "Program Files",
"size" => 4096,
"modified" => 1540579745,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Program Files (x86)",
"basename" => "Program Files (x86)",
"size" => 4096,
"modified" => 1506692707,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/ProgramData",
"basename" => "ProgramData",
"size" => 4096,
"modified" => 1539012665,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Recovery",
"basename" => "Recovery",
"size" => 0,
"modified" => 1539022717,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/System Volume Information",
"basename" => "System Volume Information",
"size" => 4096,
"modified" => 1539012967,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Users",
"basename" => "Users",
"size" => 4096,
"modified" => 1539012384,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/Windows",
"basename" => "Windows",
"size" => 24576,
"modified" => 1539011957,
"type" => "dir",
"contents" => []
],
[
"path" => "/C/pagefile.sys",
"basename" => "pagefile.sys",
"size" => 1207959552,
"modified" => 1540581479,
"type" => "file",
"contents" => []
],
[
"path" => "/C/swapfile.sys",
"basename" => "swapfile.sys",
"size" => 268435456,
"modified" => 1540581479,
"type" => "file",
"contents" => []
]
]
]
];

Я понимаю, что это можно сделать с помощью рекурсии, но мне тяжело с этим. Если бы кто-то мог указать мне правильное направление или предложить рабочее решение, это было бы действительно здорово.

0

Решение

Сортировать массив по path если он еще не заказан:

usort($inputArray, function ($a, $b) {
return $a['path'] <=> $b['path'];
});

Затем вложите элементы рекурсивно path:

function nest(&$directory)
{
for ($i = 0; $i < count($directory); $i++)
{
while ($i + 1 < count($directory) && strpos($directory[$i + 1]['path'], $directory[$i]['path'] . '/') === 0)
{
$directory[$i]['contents'][] = $directory[$i + 1];
array_splice($directory, $i + 1, 1);
}

nest($directory[$i]['contents']);
}
}

В нечувствительных к регистру файловых системах (таких как Windows) вы можете заменить strpos() с stripos(), но я не проверял это, поэтому я оставляю вам решать.

1

Другие решения

Если вы можете использовать вместо этого массив объектов, вы можете воспользоваться тем, как объекты передаются в виде ссылок, чтобы упростить создание такого рода структуры.

sort($inputArray);

foreach ($inputArray as $item) {
$item = (object) $item;
$files[$item->path] = $item;
$files[substr($item->path, 0, strrpos($item->path, '/'))]->contents[] = $item;
}

$result = reset($files);

Если объекты не будут работать для вас, JSON encode / decode может преобразовать объекты в результате в массивы.

$result = json_decode(json_encode($result), true);
1

По вопросам рекламы [email protected]