$result = array();
foreach($files as $file)
{
$directories = explode('.', $file->url);
$currentRoot = &$result ;
foreach($directories as $directory)
{
$dir = null ;
foreach($currentRoot as $i => $d)
{
if(isset($d['folder']) && $d['folder'] and $d['title'] == $directory)
{
$dir = &$currentRoot[$i] ;
break ;
}
}
if(is_null($dir))
{
$item = array(
'title' => $directory,
'folder' => true,
'children' => array()
);
$currentRoot[] = $item ;
$dir = &$currentRoot[count($currentRoot)-1];
}
$currentRoot = &$dir['children'] ;
unset($dir);
}
$currentRoot[] = array(
'title' => $file->name . '.' . $file->type,
'key' => $file->id,
);
unset($currentRoot);
}
echo json_encode($result);