递归获取嵌套树的所有 children
Get all children of a nested tree recursively
谢谢大家
我有一个 JSON 嵌套树,我只想检索 ID。
这是我查询的结果。
我的目标是调用一个递归函数并给它 parent id,它将 return 所有 children 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;
}
谢谢大家
我有一个 JSON 嵌套树,我只想检索 ID。
这是我查询的结果。
我的目标是调用一个递归函数并给它 parent id,它将 return 所有 children 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;
}