可能重复:
Recursive function to generate multidimensional array from database result
考虑以下数组。
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
)
除此之外,儿童还可以拥有无限的孩子。并非每个索引都具有相同数量的键和值。但是所有人都有父/子密钥/值。
我从这里和其他地方尝试了很多解决方案,似乎无法做到正确。