/** * Get all folders in a tree * * @return array */ public function folderTree() { $folders = $folder_array = array(); $all_folders = Folder::findAllOrdered(); // we must reindex the array first foreach ($all_folders->toArray() as $row) { $folders[$row['id']] = (array) $row; } unset($tree); // build a multidimensional array of parent > children foreach ($folders as $row) { if (array_key_exists($row['parent_id'], $folders)) { // add this folder to the children array of the parent folder $folders[$row['parent_id']]['children'][] =& $folders[$row['id']]; } // this is a root folder if ($row['parent_id'] == 0) { $folder_array[] =& $folders[$row['id']]; } } return $folder_array; }