You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP如何将扁平父子关系数组转换为嵌套层级数组?

两种嵌套层级结构的实现方案

给定的扁平数组存储了文章ID的链式父子关系,以下是两种目标结构的具体实现:

一、键值嵌套形式

这种结构会形成 父ID => [子ID => [孙ID => ...]] 的层级嵌套:

$data = array(
    677 => 678,
    678 => 679,
    679 => 880,
    880 => 881,
    881 => 882,
    882 => 883,
    883 => 884
);

// 递归构建键值嵌套结构
function buildKeyValueNested($data, $currentId) {
    // 当前ID没有子节点时,直接返回ID
    if (!isset($data[$currentId])) {
        return $currentId;
    }
    // 否则以当前ID为键,递归构建子节点结构
    return [
        $currentId => buildKeyValueNested($data, $data[$currentId])
    ];
}

// 定位根节点:在数组键中但不在值中的ID(即最顶层父节点)
$values = array_values($data);
$root = null;
foreach (array_keys($data) as $key) {
    if (!in_array($key, $values)) {
        $root = $key;
        break;
    }
}

// 生成结果
$keyValueResult = buildKeyValueNested($data, $root);
print_r($keyValueResult);

输出结果示例:

Array
(
    [677] => Array
        (
            [678] => Array
                (
                    [679] => Array
                        (
                            [880] => Array
                                (
                                    [881] => Array
                                        (
                                            [882] => Array
                                                (
                                                    [883] => 884
                                                )
                                        )
                                )
                        )
                )
        )
)

二、含id和child键的嵌套形式

这种结构每个节点以 ['id' => 节点ID, 'child' => 子节点结构] 的格式存在:

// 递归构建id+child嵌套结构
function buildIdChildNested($data, $currentId) {
    $node = ['id' => $currentId];
    // 如果当前ID有子节点,递归生成child结构;否则设为空数组
    if (isset($data[$currentId])) {
        $node['child'] = buildIdChildNested($data, $data[$currentId]);
    } else {
        $node['child'] = [];
    }
    return $node;
}

// 生成结果(复用之前找到的$root)
$idChildResult = buildIdChildNested($data, $root);
print_r($idChildResult);

输出结果示例:

Array
(
    [id] => 677
    [child] => Array
        (
            [id] => 678
            [child] => Array
                (
                    [id] => 679
                    [child] => Array
                        (
                            [id] => 880
                            [child] => Array
                                (
                                    [id] => 881
                                    [child] => Array
                                        (
                                            [id] => 882
                                            [child] => Array
                                                (
                                                    [id] => 883
                                                    [child] => Array
                                                        (
                                                            [id] => 884
                                                            [child] => Array
                                                                (
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
)

补充说明

如果你的数据存在多个根节点或分支结构,只需调整根节点的查找逻辑(收集所有不在值中的键),并循环调用构建函数即可。

内容的提问来源于stack exchange,提问作者Dinesh Chandra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:55:24