Reputation: 1662
Hello I Have this Array from a dataset:
Example:
array (
1 =>
array (
'id' => '1',
'name' => ' Category',
'id_parent' => '0',
'ativo' => '1',
),
2 =>
array (
'id' => '2',
'name' => ' Slippers',
'id_parent' => '0',
'ativo' => '1',
),
3 =>
array (
'id' => '3',
'name' => ' TShirts',
'id_parent' => '0',
'ativo' => '1',
),
4 =>
array (
'id' => '4',
'name' => ' BlousesSweatshirt',
'id_parent' => '0',
'ativo' => '1',
),
5 =>
array (
'id' => '5',
'name' => ' Cap',
'id_parent' => '0',
'ativo' => '1',
),
6 =>
array (
'id' => '6',
'name' => ' Stickers',
'id_parent' => '0',
'ativo' => '1',
),
7 =>
array (
'id' => '7',
'name' => ' ScreensandFrames',
'id_parent' => '0',
'ativo' => '1',
),
8 =>
array (
'id' => '8',
'name' => ' Models',
'id_parent' => '0',
'ativo' => '1',
),
9 =>
array (
'id' => '9',
'name' => ' Notebook',
'id_parent' => '6',
'ativo' => '1',
),
10 =>
array (
'id' => '10',
'name' => ' Door',
'id_parent' => '0',
'ativo' => '1',
),
11 =>
array (
'id' => '11',
'name' => ' Door',
'id_parent' => '6',
'ativo' => '1',
),
12 =>
array (
'id' => '12',
'name' => ' Kangaroo',
'id_parent' => '4',
'ativo' => '1',
),
13 =>
array (
'id' => '13',
'name' => ' KangarooRaglan',
'id_parent' => '4',
'ativo' => '1',
),
14 =>
array (
'id' => '14',
'name' => ' RoundCollar',
'id_parent' => '4',
'ativo' => '1',
),
15 =>
array (
'id' => '15',
'name' => ' Trucker',
'id_parent' => '5',
'ativo' => '1',
),
16 =>
array (
'id' => '16',
'name' => ' Basic',
'id_parent' => '3',
'ativo' => '1',
),
17 =>
array (
'id' => '17',
'name' => ' Longline',
'id_parent' => '3',
'ativo' => '1',
),
18 =>
array (
'id' => '18',
'name' => ' Raglan',
'id_parent' => '3',
'ativo' => '1',
),
19 =>
array (
'id' => '19',
'name' => ' Raglan3/4',
'id_parent' => '3',
'ativo' => '1',
),
20 =>
array (
'id' => '20',
'name' => ' Regatta',
'id_parent' => '3',
'ativo' => '1',
),
21 =>
array (
'id' => '21',
'name' => ' Slide',
'id_parent' => '2',
'ativo' => '1',
),
22 =>
array (
'id' => '22',
'name' => ' Stickers',
'id_parent' => '8',
'ativo' => '1',
),
23 =>
array (
'id' => '23',
'name' => ' Notebook',
'id_parent' => '22',
'ativo' => '1',
),
24 =>
array (
'id' => '24',
'name' => ' T-shirt',
'id_parent' => '8',
'ativo' => '1',
),
25 =>
array (
'id' => '25',
'name' => ' Basic',
'id_parent' => '24',
'ativo' => '1',
),
26 =>
array (
'id' => '26',
'name' => ' Slippers',
'id_parent' => '8',
'ativo' => '1',
),
27 =>
array (
'id' => '27',
'name' => ' Slide',
'id_parent' => '26',
'ativo' => '1',
),
28 =>
array (
'id' => '28',
'name' => ' 1Screen',
'id_parent' => '7',
'ativo' => '1',
),
29 =>
array (
'id' => '29',
'name' => ' Set3Screens',
'id_parent' => '7',
'ativo' => '1',
),
30 =>
array (
'id' => '30',
'name' => ' Set5Screens',
'id_parent' => '7',
'ativo' => '1',
),
31 =>
array (
'id' => '31',
'name' => ' BlousesSweatshirt',
'id_parent' => '8',
'ativo' => '1',
),
32 =>
array (
'id' => '32',
'name' => ' Cap',
'id_parent' => '8',
'ativo' => '1',
),
33 =>
array (
'id' => '33',
'name' => ' ScreensandFrames',
'id_parent' => '8',
'ativo' => '1',
),
)
Whereas this array we have fathers and childs,
example:
-EX:[1] Models is father of: Stickers, T-shirt, Slippers
-EX:[2] Stickers is father of: Notebook
What I need is a function to get Model and all Childrens (till end) recursively:
I've already done a function to get Recursive Fathers from the Notebook, for example:
here:
function returnParent($id, $haystack, $arr = null){
$needle = $haystack[$id];
$arr[] = $needle;
if($needle['id_parent']){
return returnParent($needle['id_parent'], $haystack, $arr);
}else{
return $arr;
}
}
where $id = 23
and $haystack
is complete dataset.
result:
Array
(
[0] => Array
(
[id] => 23
[name] => Notebook
[id_parent] => 22
)
[1] => Array
(
[id] => 22
[name] => Stickers
[id_parent] => 8
)
[2] => Array
(
[id] => 8
[name] => models
[id_parent] => 0
)
)
The result of function to return childrens needs to be the same of returnParent, the looked up value need to be in array as key 0;
complete dataset here: https://pastebin.com/jgTM7aLA
suggestions to make my function better is very wellcome :)
EDIT
Expected output:
0 =>
array (
'id' => '8',
'name' => ' Models',
'id_parent' => '0',
'ativo' => '1',
),
1 =>
array (
'id' => '6',
'name' => ' Stickers',
'id_parent' => '0',
'ativo' => '1',
),
2 =>
array (
'id' => '9',
'name' => ' Notebook',
'id_parent' => '6',
'ativo' => '1',
),
3 =>
array (
'id' => '24',
'name' => ' T-shirt',
'id_parent' => '8',
'ativo' => '1',
),
4 =>
array (
'id' => '25',
'name' => ' Basic',
'id_parent' => '24',
'ativo' => '1',
)... and ..etc
Something like that, the key 0 must be the searched value, and the others values, dont need to be in tree order, any order its ok, as long as get all values.
in other words:
get a array node, show him and all childrens and grandchildrens.
Upvotes: 1
Views: 2153
Reputation: 562
function returnChild($id, $haystack, $arr = []){
$needle = $haystack[$id];
array_push($arr, $needle);
foreach($haystack as $key){
if($key['id_parent'] == $needle['id']){
array_push($arr,returnChild($key['id'], $haystack));
}
}
return $arr;
}
I hope this is the solution to your problem!
Upvotes: 2
Reputation: 23958
I believe this is what you are looking for.
I loop the unique parents and find what items have a matching parent.
I had to add a "NULL" value to parents and IDs because your array is not zero indexed.
That took me a long time to figure out...
Anyways the return is a 'parent' array and children below.
$temp = array_column($arr, "id");
$ids[0] = "NULL";
$ids = array_merge($ids, $temp);
$temp = array_column($arr, "id_parent");
$parents[0] = "NULL";
$parents = array_merge($parents, $temp);
Foreach(array_unique($parents) as $parent){
if($parent ==0) continue; // if parent is 0 we don't need to search for it (I think)
$new[$parent]['parent'] = $arr[$parent];
$new[$parent]['children'] = array_intersect_key($arr, array_intersect($parents, [$parent]));
}
Var_dump($new);
Upvotes: 1