递归地获取嵌套树的所有子级



感谢大家
我有一个JSON嵌套树,我只想检索ID
这是我查询的结果
我的目标是调用一个recosrive函数,并给它父id,它将返回所有子id
Ps:我正在与laravel、Eloquent合作

[{
"id": "1",
"name": "albert",
"children": [{
    "id": "5",
    "name": "George",
    "children": [{
        "id": "7",
        "name": "Julia"
    }, {
        "id": "9",
        "name": "Harry",
        "children": []
    }]
}, {
    "id": "2",
    "name": "Richard",
    "children": []
    }]
}]

我试图制作一个递归函数,但它不起作用。

function displayArrayRecursively($childrenUnit) {
foreach ($childrenUnit as $unit) {
    $ids[] = $unit->id;
    if ($unit->childrenUnit) {
        displayArrayRecursively(Unit::find($unit->id)->childrenUnit);
    } else {
        return $ids;
    }
}}

任何帮助

这似乎对我有效

<?php
$jsonString = '[{
"id": "1",
"name": "albert",
"children": [{
    "id": "5",
    "name": "George",
    "children": [{
        "id": "7",
        "name": "Julia"
    }, {
        "id": "9",
        "name": "Harry",
        "children": []
    }]
}, {
    "id": "2",
    "name": "Richard",
    "children": []
    }]
}]';
$objects = json_decode($jsonString);
function findIds($child, $ids) {
    if (isset($child->children)) {
        if (count($child->children) > 0) {
            foreach ($child->children as $ch) {
                $ids[] = $ch->id;
                $ids = findIds($ch, $ids);
            }
        }
    }
    return $ids;
}
$result = array();
if (count($objects) > 0) {
    $ids = array();
    foreach ($objects as $object) {
        $ids = findIds($object, array());
        $result[] = array('id' => $object->id, 'ids' => $ids);
    }
}
echo '<pre>';
print_r($result);
echo '</pre>';

输入:

Array
(
    [0] => Array
        (
            [name] => Query Reporting and Analytics
            [id] => 598
            [children] => Array
                (
                    [605] => Array ([name] => Access query [id] => 605 )
                    [606] => Array ([name] => Powerful [id] => 606)    
                )
        )
    [1] => Array
        (
            [name] => Business Intelligence Platform
            [id] => 599
            [children] => Array
                (
                    [849] => Array ([name] => Access, explore [id] => 849)
                    [850] => Array ([name] => Advanced reporting[id] => 850)
                )
        )
    [2] => Array
        (
            [name] => Data Management
            [id] => 600
        )

输出:

Array
(
    [0] => 598
    [1] => 605
    [2] => 606
    [3] => 599
    [4] => 849
    [5] => 850
    [6] => 600    
)

检查以下函数以获取树中的ID。

function findTreeIds($tree, $ids = array(), $colName = 'id', $childColName = 'children')
    {
        foreach ($tree as $element) {
            if (!isset($element[$colName])) {
                continue;
            }
            $ids[] = $element[$colName];
            if (isset($element[$childColName]) && count($element[$childColName]) > 0) {
                $ids = static::findTreeIds($element[$childColName], $ids, $colName, $childColName);
            }
        }
        return $ids;
    }

相关内容

  • 没有找到相关文章

最新更新