php 在PHP中使用平面数组构建树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 在PHP中使用平面数组构建树相关的知识,希望对你有一定的参考价值。

{
    "5a969a6fe799f7cbe9083122": {
        "_id": "5a969a6fe799f7cbe9083122",
        "name": "Tajawal",
        "parent": null,
        "created_by": "5a8d84b8e799f70a157f8211",
        "update_at": "1519819375891",
        "created_at": "1519819375891",
        "children": {
            "5a969f5ce799f7cbe9083123": {
                "_id": "5a969f5ce799f7cbe9083123",
                "name": "Tajawal UAE",
                "parent": "5a969a6fe799f7cbe9083122",
                "created_by": "5a8d84b8e799f70a157f8211",
                "update_at": "1519820745535",
                "created_at": "1519820636332",
                "children": {
                    "5a96a622e799f7cbe9083129": {
                        "_id": "5a96a622e799f7cbe9083129",
                        "name": "Testing child",
                        "parent": "5a969f5ce799f7cbe9083123",
                        "created_by": "5a8d84b8e799f70a157f8211",
                        "update_at": "1519822370999",
                        "created_at": "1519822370999"
                    }
                }
            },
            "5a969ff6e799f7cbe9083126": {
                "_id": "5a969ff6e799f7cbe9083126",
                "name": "Tajawal Dubai",
                "parent": "5a969a6fe799f7cbe9083122",
                "created_by": "5a8d84b8e799f70a157f8211",
                "update_at": "1519820790199",
                "created_at": "1519820790199"
            }
        }
    },
    "5a96a5c4e799f7cbe9083127": {
        "_id": "5a96a5c4e799f7cbe9083127",
        "name": "Android",
        "parent": null,
        "created_by": "5a8d84b8e799f70a157f8211",
        "update_at": "1519822295399",
        "created_at": "1519822276992"
    }
}
<?php

$data = [
    [
        "_id" => "5a969a6fe799f7cbe9083122",
        "name" => "Tajawal",
        "parent" => null,
        "created_by" => "5a8d84b8e799f70a157f8211",
        "update_at" => "1519819375891",
        "created_at" => "1519819375891"
    ],
    [
        "_id" => "5a969f5ce799f7cbe9083123",
        "name" => "Tajawal UAE",
        "parent" => "5a969a6fe799f7cbe9083122",
        "created_by" => "5a8d84b8e799f70a157f8211",
        "update_at" => "1519820745535",
        "created_at" => "1519820636332"
    ],
    [
        "_id" => "5a969ff6e799f7cbe9083126",
        "name" => "Tajawal Dubai",
        "parent" => "5a969a6fe799f7cbe9083122",
        "created_by" => "5a8d84b8e799f70a157f8211",
        "update_at" => "1519820790199",
        "created_at" => "1519820790199"
    ],
    [
        "_id" => "5a96a5c4e799f7cbe9083127",
        "name" => "Android",
        "parent" => null,
        "created_by" => "5a8d84b8e799f70a157f8211",
        "update_at" => "1519822295399",
        "created_at" => "1519822276992"
    ],
    [
        "_id" => "5a96a622e799f7cbe9083129",
        "name" => "Testing child",
        "parent" => "5a969f5ce799f7cbe9083123",
        "created_by" => "5a8d84b8e799f70a157f8211",
        "update_at" => "1519822370999",
        "created_at" => "1519822370999"
    ]
];

function buildTree(array &$elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[$element['id']] = $element;
            unset($elements[$element['id']]);
        }
    }
    return $branch;
}

var_dump(buildTree($data));

以上是关于php 在PHP中使用平面数组构建树的主要内容,如果未能解决你的问题,请参考以下文章

如何有效地从 php 中的平面结构构建树?

PHP从遍历嵌套树模型构建数组

从平面对象数组构建对象树数组[重复]

如何使用 php 获取所有可能的决策树

从平面数据在 PHP 中构建分层 html 标签

平面文件从数组中选择值