可能重复:
从数据库结果生成多维数组的递归函数
考虑以下数组。
Array
(
[0] => Array
(
[Parent_ID] => 0
[Child_ID] => 0
[0] => string:val
)
[1] => Array
(
[Parent_ID] => 0
[Child_ID] => 1
[0] => string:val
)
[2] => Array
(
[Parent_ID] => 1
[Child_ID] => 2
[0] => string:val
)
[3] => Array
(
[Parent_ID] => 0
[Child_ID] => 3
[0] => string:val
)
我需要把它变成如下内容。
Array
(
[0] => Array
(
[Parent_ID] => 0
[Child_ID] => 0
[0] => string:val
)
[1] => Array
(
[Parent_ID] => 0
[Child_ID] => 1
[0] => string:val
[2] => Array
(
[Parent_ID] => 1
[Child_ID] => 2
[0] => string:val
)
)
[3] => Array
(
[Parent_ID] => 0
[Child_ID] => 3
[0] => string:val
)
除此之外,孩子们也可以拥有无限的孩子。并不是每个索引都有相同数量的键和值。但是所有人都会有一个父子键/值。
我从这里和其他地方尝试了许多解决方案,但似乎无法做到正确。