如何重新排列如下數組PHP重新排序數組以反映父/ ID層次
[0] => Array
(
[id] => 1
[parent_id] => 0
[name] => Accueil
)
[1] => Array
(
[id] => 2
[parent_id] => 0
[name] => Exposants
)
[2] => Array
(
[id] => 3
[parent_id] => 0
[name] => Visiteurs
)
[3] => Array
(
[id] => 4
[parent_id] => 0
[name] => Medias
)
[4] => Array
(
[id] => 5
[parent_id] => 0
[name] => Activités
)
[5] => Array
(
[id] => 6
[parent_id] => 1
[name] => Contact
)
[6] => Array
(
[id] => 7
[parent_id] => 3
[name] => Partenaires
)
[7] => Array
(
[id] => 8
[parent_id] => 2
[name] => News
)
於是我想出了一個反映層次結構如圖中的ID和PARENT_ID領域的陣列?數組鍵是數組元素的ID字段是父項。在這個數組裏面每次都是一個以ID字段爲關鍵字的子數組。示例:
[1] => Array
(
[name] => Accueil
[children] => array(
[0] => bla,
[3] => bla2
)
)
[2] => Array
(
[name] => Something
[children] => array(
[4] => bla3,
)
)
如果你在主數組中有所有的「人員」,將這些ID單獨存儲在「子」鍵下而不是將其作爲鍵/值存儲是不明智的(假設該值是名稱或某些)? – Orbling 2010-11-18 16:12:29