/** * Folder folders by their order * * @param array $folders the folders to sort * @param int $parent_guid the parent to sort for * * @return bool|array */ function file_tools_sort_folders($folders, $parent_guid = 0) { $result = false; if (array_key_exists($parent_guid, $folders)) { $result = array(); foreach ($folders[$parent_guid] as $subfolder) { $children = file_tools_sort_folders($folders, $subfolder->getGUID()); $order = $subfolder->order; if (empty($order)) { $order = 0; } while (array_key_exists($order, $result)) { $order++; } $result[$order] = array("folder" => $subfolder, "children" => $children); } ksort($result); } return $result; }
/** * Folder folders by their order * * @param array $folders the folders to sort * @param int $parent_guid the parent to sort for * * @return false|array */ function file_tools_sort_folders($folders, $parent_guid = 0) { if (!array_key_exists($parent_guid, $folders)) { return false; } $result = []; /* @var $subfolder ElggObject */ foreach ($folders[$parent_guid] as $subfolder) { $children = file_tools_sort_folders($folders, $subfolder->getGUID()); $order = (int) $subfolder->order; if (empty($order)) { $order = $subfolder->time_created; } while (array_key_exists($order, $result)) { $order++; } $result[$order] = ['folder' => $subfolder, 'children' => $children]; } ksort($result); return $result; }