I am trying to get multi-dimensional array for directories formatted as below :
[
{
"text": "another_folder",
"href": "gui/default/uploads/another_folder",
"depth": 0
},
{
"text": "subfold",
"href": "gui/default/uploads/subfold",
"depth": 0,
"nodes": {
"text": "sub-subfold",
"href": "gui/default/uploads/subfold/sub-subfold",
"depth": 1,
}
}
]
I want to use RecursiveIterators. What I have done so far is I am getting all directories listed in given path. I need to go inside to children which is where I stacked.
public function list_folders($folder_path='') {
if(!$folder_path) $folder_path = $this->upl_path;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folder_path),
RecursiveIteratorIterator::SELF_FIRST);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$r = array();
$counter = 0
foreach ($iterator as $splFileInfo) {
if($splFileInfo->isDir()) {
$r[$counter] = array(
'text' => $splFileInfo->getFilename(),
'href' => str_replace('\','/',$splFileInfo->getPathname())
);
if(How to check if it has children) {
$result[$counter] += array('nodes'=> CALL RECURSIVE HERE ? );
}
$counter++;
}
echo json_encode($r,JSON_PRETTY_PRINT);
}
I'd use any idea or help gladly.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…