以递归方式获取嵌套树的所有子项

时间:2015-11-19 10:11:18

标签: php laravel recursion recursive-query

谢谢大家 我有一个JSON嵌套树,我想只检索ID 这是我查询的结果 我的目标是调用recusrive函数并为其提供父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;
    }
}}

任何帮助

2 个答案:

答案 0 :(得分:1)

这似乎对我有用

<?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>';

答案 1 :(得分:0)

输入:

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;
    }