I want to build a Json output to list folder structure inside ExtJS tree panel. The structure should be the equivalent of this array in Json:
Array
(
[text] => .
[children] => Array
(
[0] => Array
(
[text] => files
[children] =>
)
[1] => Array
(
[text] => folder 1
[children] =>
)
[2] => Array
(
[text] => New directory
[children] => array(
[0] => Array
(
[text] => sub_1
[children] => array(
[0] => Array
(
[text] => sub_1_1
[children] =>
)
[1] => Array
(
[text] => sub_1_2
[children] =>
)
)
)
[1] => Array
(
[text] => sub_2
[children] =>
)
)
)
)
)
i made this function which shows the structure by going through PHP Manual and examples
listFolders('../file_uploads/');
function listFolders($dir){
$dh = scandir($dir);
echo '<ul>';
foreach($dh as $folder){
if($folder != '.' && $folder != '..')
{
if(is_dir($dir.'/'.$folder)){
echo '<li>'.$folder.'</li>';
listFolders($dir.'/'.$folder);
}
}
}
echo '</ul>';
}
this outputs the structure
- New directory
- sub_1
- sub_1_1
- sub_1_1_1
- sub_1_2
- sub_2
- files
- folder 1
I want to know how to convert this output to a array (or the Json) like above?
solution
print "<pre>";
print_r(listFolders('../file_uploads/'));
function listFolders($dir)
{
$dh = scandir($dir);
$return = array();
foreach ($dh as $folder) {
if ($folder != '.' && $folder != '..') {
if (is_dir($dir . '/' . $folder)) {
$return[] = array(
'text' => $folder,
'children' => listFolders($dir . '/' . $folder)
);
}
}
}
return $return;
}

Try the following code:
Seems to work for me, not tested properly though.