我有以下陣列:(由PARENT_ID訂購)排序無限深度陣列 - PHP
array(9) {
[1]=>
array(3) {
["name"]=>
string(6) "Tennis"
["parent_id"]=>
NULL
["depth"]=>
int(0)
}
[7]=>
array(3) {
["name"]=>
string(11) "TOP LEVEL 2"
["parent_id"]=>
NULL
["depth"]=>
int(0)
}
[8]=>
array(3) {
["name"]=>
string(11) "TOP LEVEL 3"
["parent_id"]=>
NULL
["depth"]=>
int(0)
}
[2]=>
array(3) {
["name"]=>
string(5) "Shoes"
["parent_id"]=>
string(1) "1"
["depth"]=>
int(1)
}
[3]=>
array(3) {
["name"]=>
string(5) "Women"
["parent_id"]=>
string(1) "2"
["depth"]=>
int(2)
}
[4]=>
array(3) {
["name"]=>
string(4) "Mens"
["parent_id"]=>
string(1) "2"
["depth"]=>
int(2)
}
[5]=>
array(3) {
["name"]=>
string(12) "Mens Running"
["parent_id"]=>
string(1) "4"
["depth"]=>
int(3)
}
[6]=>
array(3) {
["name"]=>
string(11) "Mens Tennis"
["parent_id"]=>
string(1) "4"
["depth"]=>
int(3)
}
[9]=>
array(3) {
["name"]=>
string(9) "2nd level"
["parent_id"]=>
string(1) "8"
["depth"]=>
int(1)
}
}
我想它的方式,它可以在下拉菜單中使用排序。下拉菜單的格式爲:
$categories[$CATEGORY_ID] = str_repeat(' ', $value['depth']).$value['name'];
上面的數組頂部按parent_id排序,其中頂級的parent_id爲NULL。
我需要按照數組按順序排序。例如:
[1] => 'Tennis';
[2] => '  Shoes'
[3] => ' Womens'
[4] => ' Men'
[5] => ' Mens Running
[6] => ' Mens Tennis
[7] => 'TOP LEVEL 2'
[8] => 'TOP LEVEL 3'
[9] => ' 2nd level'
我嘗試這樣做:
function get_all_categories_and_subcategories($parent_id = NULL, $depth = 0)
{
$categories = $this->get_all($parent_id, 10000, 0, 'parent_id');
if (!empty($categories))
{
$unique_parent_ids = array();
foreach($categories as $id => $value)
{
$categories[$id]['depth'] = $depth;
$unique_parent_ids[$id] = TRUE;
}
foreach(array_keys($unique_parent_ids) as $id)
{
$categories = array_replace($categories, $this->get_all_categories_and_subcategories($id, $depth + 1));
}
return $categories;
}
else
{
return $categories;
}
}
function sort_categories($categories)
{
usort($categories, array('Category','do_sort_categories'));
return $categories;
}
static function do_sort_categories($a, $b)
{
$al = strtolower($a['parent_id']);
$bl = strtolower($b['parent_id']);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}
function get_all($parent_id = NULL, $limit=10000, $offset=0,$col='name',$order='asc')
{
$this->db->from('categories');
if (!$this->config->item('speed_up_search_queries'))
{
$this->db->order_by($col, $order);
}
if ($parent_id === NULL)
{
$this->db->where('parent_id IS NULL', null, false);
}
else if($parent_id)
{
$this->db->where('parent_id', $parent_id);
}
$this->db->limit($limit);
$this->db->offset($offset);
$return = array();
foreach($this->db->get()->result_array() as $result)
{
$return[$result['id']] = array('name' => $result['name'], 'parent_id' => $result['parent_id']);
}
return $return;
}
你到目前爲止試過了什麼?這不是一個編碼服務,所以你需要首先展示一個可靠的嘗試。我建議你閱讀[如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 – Anonymous
我試過ksort;但是這隻有在類別按順序創建時纔有效。我也試圖使用usort;但無法弄清楚。 –
我已更新我的代碼。我試圖發佈代碼的隔離部分,因爲我已經完成了數據庫中的檢索部分;但也許可以修改? –