'Make a recursion on array to find if children of parent has also children [PHP]
Ex:
[
[1] => [
[2],
[3],
[4]
],
[4] => [
[5],
[6],
[7]
]
]
how do i loop through the array and find the child(ren) that needs to be attached to they parent child?
the 4 parent needs to be attach to 4 child inside 1.
Solution 1:[1]
If any of children of a parent has also children?
$input = [
1 => [2, 3, 4],
4 => [5, 6],
2 => [4, 6]
];
$allparents = array_keys($input);
foreach ($allparents as $parent) {
foreach ($input as $nodes) {
if (in_array($parent, $nodes)) {
$out[] = $parent;
}
}
}
$out = array_unique($out);
print_r($out);
will print:
Array
(
[0] => 4
[2] => 2
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ersin |
