i am trying to re format a array as tree, main array and child arrays should be grouped by "name" property.
my flat array is like
$flat = [
['id' => 1, 'parent_id' => 0, 'name' => 'root1'],
['id' => 2, 'parent_id' => 0, 'name' => 'root1'],
['id' => 3, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 4, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 5, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 6, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 7, 'parent_id' => 0, 'name' => 'root2'],
['id' => 8, 'parent_id' => 0, 'name' => 'root2'],
['id' => 9, 'parent_id' => 7, 'name' => 'ch3-1'],
['id' => 10, 'parent_id' => 7, 'name' => 'ch3-1']
];
i could build tree structure by
$tree = buildTree($flat, 'parent_id', 'id');
function buildTree(array $flatList)
{
$grouped = [];
foreach ($flatList as $node) {
$grouped[$node['parent_id']][] = $node;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) {
foreach ($siblings as $k => $sibling) {
$id = $sibling['id'];
if (isset($grouped[$id])) {
$sibling['children'] = $fnBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
return $fnBuilder($grouped[0]);
}
this works perfectly.
But what i want is build nested array should be grouped by it's "name" property.
so the final output should be like
[
"root1": [{
"id": 1,
"parent_id": 0,
"name": "root1",
"children": [
"ch-1": [{
"id": 3,
"parent_id": 1,
"name": "ch-1",
"children": [
"ch-1-1": [{
"id": 5,
"parent_id": 3,
"name": "ch-1-1"
},
{
"id": 6,
"parent_id": 3,
"name": "ch-1-1"
}
]
]
},
{
"id": 4,
"parent_id": 1,
"name": "ch-1"
}
]
]
},
{
"id": 2,
"parent_id": 0,
"name": "root1"
}
],
"root2": [{
"id": 7,
"parent_id": 0,
"name": "root2",
"children": [
"ch3-1": [{
"id": 9,
"parent_id": 7,
"name": "ch3-1"
},
{
"id": 10,
"parent_id": 7,
"name": "ch3-2"
}
]
]
},
{
"id": 8,
"parent_id": 0,
"name": "root2"
}
]
]
I have been stuck here almost for couple of days. Please help me to solve this problem.
Thank you.
My Answer:
Here is an example of how you can build a grouped tree array from a flat array in PHP:
php
// Sample flat array
$flatArray = [
['id' => 1, 'name' => 'Parent 1', 'parent_id' => null],
['id' => 2, 'name' => 'Child 1', 'parent_id' => 1],
['id' => 3, 'name' => 'Child 2', 'parent_id' => 1],
['id' => 4, 'name' => 'Parent 2', 'parent_id' => null],
['id' => 5, 'name' => 'Child 3', 'parent_id' => 4],
];
// Initialize empty tree array
$treeArray = [];
// Group flat array by parent_id
$groupedArray = [];
foreach ($flatArray as $item) {
$groupedArray[$item['parent_id']][] = $item;
}
// Build tree array
foreach ($groupedArray as $parentId => $children) {
foreach ($children as $child) {
if ($parentId === null) {
$treeArray[] = $child;
} else {
foreach ($treeArray as &$parent) {
if ($parent['id'] === $parentId) {
$parent['children'][] = $child;
}
}
}
}
}
// Output tree array
print_r($treeArray);
In this example, we first group the flat array by the `parent_id` using a foreach loop. Then, we iterate over the grouped array to build the tree array by adding children to their respective parent nodes. Finally, we output the tree array using `print_r()`.
Rate this post
5 of 5 based on 1086 votesComments