У меня есть функция PHP для создания дерева каталогов, и я не могу отформатировать результат, чтобы получить файл JSON, например:
[
{
text: 'Parent',
href: 'parent/',
nodes: [
{
text: 'Child',
href: 'child/',
nodes: [
{
text: 'Grandchild',
href: 'grandchild/',
},
{
text: 'Grandchild',
href: 'grandchild/',
}
]
},
{
text: 'Child',
href: 'child/',
}
]
},
{
text: 'Parent',
href: 'parent/',
},
{
text: 'Parent',
href: 'parent/',
},
{
text: 'Parent',
href: 'parent/',
},
{
text: 'Parent',
href: 'parent/',
},
{
text: 'Parent',
href: 'parent/',
nodes: [
{
text: 'Child',
href: 'child/',
nodes: [
{
text: 'Grandchild',
href: 'grandchild/',
},
{
text: 'Grandchild',
href: 'grandchild/',
}
]
},
{
text: 'Child',
href: 'child/',
}
]
}
]
Здесь моя функция PHP, кто-то может помочь меня? Спасибо
function scandir_rec($root)
{
$data = [];
if (!is_dir($root)) {
return;
}
$dirs = scandir($root);
foreach ($dirs as $dir) {
if ($dir == '.' || $dir == '..') {
continue;
}
$path = $root . '/' . $dir;
$data[] = ['text'=>$dir, 'link'=>urlencode($path)];
if (is_dir($path)) {
$data[] = ['nodes' => scandir_rec($path)];
}
}
return json_encode($data, JSON_UNESCAPED_SLASHES);
}
// init call
$rootDir = '/var/usr';
scandir_rec($rootDir);