如何使用 php 将 array#1 转换为 array#2 结构?
第一个数组是对生物体列表进行数据库查询的结果,每个生物体都按其目、科、属、种进行分类。Hierarchically Species 是各种 Genus 的子分类,Genus 分类是各种 Families 等的子分类。
在命名空间术语中,您可以像这样阅读它:
索引项[0] ---> Hemiptera.Miridae.Kanakamiris
索引项[1] ---> Hemiptera.Miridae.Neophloeobia.incisa
array#1 的键之间存在一种父/子关系,如下所示:
- 'Rank_Order' 值是 'Rank_Family' 值的父级
- 'Rank_Family' 值是 'Rank_Genus' 值的父级
- 'Rank_Genus' 值是 'Rank_Species' 值的父级
数组#1:
Array
(
[0] => Array
(
['Rank_Order'] => 'Hemiptera'
['Rank_Family'] => 'Miridae'
['Rank_Genus'] => 'Kanakamiris'
['Rank_Species'] => ''
)
[1] => Array
(
['Rank_Order'] => 'Hemiptera'
['Rank_Family'] => 'Miridae'
['Rank_Genus'] => 'Neophloeobia'
['Rank_Species'] => 'incisa'
)
[2] => Array
(
['Rank_Order'] => 'Hemiptera'
['Rank_Family'] => 'Noridae'
['Rank_Genus'] => 'Canelbia'
['Rank_Species'] => 'Arissa'
)
)
以下数组是我需要的数组结构:array#2:
Array(
[name] => 'Hemiptera'
[children] => Array(
[0] => Array(
[name] => 'Miridae'
[children] => Array(
[0] => Array(
[name] => 'Kanakamiris'
[children] => Array(
)
)
[1] => Array(
[name] => 'Neophloeobia'
[children] => Array(
[0] => Array(
[name] => 'incisa'
[children] => Array(
)
)
)
)
)
)
[1] => Array(
[name] => 'Noridae'
[children] => Array(
[0] => Array(
[name] => 'Canelbia'
[children] => Array(
[0] => Array(
[name] => 'Arissa'
[children] => Array(
)
)
)
)
)
)
)
)
我在堆栈溢出中看到了类似的问题,尽管在我的情况下无法使用它们。例如。php-reorder-array-to-reflect-parent-id-hierarchy