is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don't know how to make it more efficient. is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don't know how to make it more efficient.
That is the array:
array (
'pole number 1' =>
array (
0 => 'pole number 1',
1 => 'TRUE',
'children' =>
array (
'pole number 3' =>
array (
0 => 'pole number 3',
1 => 'FALSE',
2 => 'pole number 1',
'children' =>
array (
'pole number 2' =>
array (
0 => 'pole number 2',
1 => 'FALSE',
2 => 'pole number 3',
),
),
),
'pole number 4' =>
array (
0 => 'pole number 4',
1 => 'FALSE',
2 => 'pole number 1',
),
),
),
'pole number 5' =>
array (
0 => 'pole number 5',
1 => 'TRUE',
'children' =>
array (
'pole number 6' =>
array (
0 => 'pole number 6',
1 => 'FALSE',
2 => 'pole number 5',
'children' =>
array (
'pole number 7' =>
array (
0 => 'pole number 7',
1 => 'FALSE',
2 => 'pole number 6',
),
),
),
'pole number 8' =>
array (
0 => 'pole number 8',
1 => 'FALSE',
2 => 'pole number 5',
),
),
),
)
1
--3
---2
--4
Expected Output
[
[1,3,2],
[1,4]
]
foreach ($tree as $key => $value) {
$result = traverseTree($value);
}
function traverseTree($tree = array())
{
if ($tree[1] && !empty($tree['children'])) {
// if no parent id than that is root
$result[$tree[0]] = $tree[0];
traverseTree($tree['children']);
} else {
foreach ($tree as $key => $node) {
var_dump($node[1]);
if ($node[1]) {
$result[$node[2]][] = $node[0];
}
}
}
var_dump($result);
}