【发布时间】:2025-11-28 01:20:10
【问题描述】:
我有一个嵌套数组,其结构如下所述。
这是一个组织结构图,其中一名雇员可能与其他雇员相关:
$tree_array = [
[
'id' => 1,
'employee' => 'John',
'leader_id' => NULL,
'team' => [
[
'id' => 2,
'employee' => 'Maria',
'leader_id' => 1,
'team' => [],
],
[
'id' => 3,
'employee' => 'Kevin',
'leader_id' => 1,
'team' => [
[
'id' => 4,
'employee' => 'Alan',
'leader_id' => 3,
'team' => [],
],
[
'id' => 5,
'employee' => 'Bret',
'leader_id' => 3,
'team' => [],
],
],
],
],
]
];
每个节点都有一个 ID 和一个团队,可以是其他节点的数组等等。
我需要获取平面数组中的每个节点,像这样的结构:
$flat_array = array(
array(
'id' => 1,
'employee' => 'John',
'leader_id' => NULL
),
array(
'id' => 2,
'employee' => 'Maria',
'leader_id' => 1
),
array(
'id' => 3,
'employee' => 'Kevin',
'leader_id' => 1
),
...
);
我试图实现一个递归函数,但我只得到了最后一次迭代的节点。
有什么帮助吗?
【问题讨论】: