1

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.

4
  • 1
    what is the expected output? Commented Jul 21, 2018 at 15:10
  • 1
    Are you asking for a function to output the parent and then its children in the given format? Commented Jul 21, 2018 at 15:20
  • @Andreas Expected output added! Commented Jul 21, 2018 at 16:18
  • @Krishanu, yes. Commented Jul 21, 2018 at 16:18

2 Answers 2

2
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!

Sign up to request clarification or add additional context in comments.

6 Comments

very close, what i need but need to return: T-shirt and his childeren Basic, Slippers and his childeren Slide, BlousesSweatshirt,, Cap, ScreensandFrames, in other words.. everything under Models
look this image everything under id 8 "modelos" paste.pics/018ae28d239f1452a01e9b2fdeda2ff1
so you want the child nested to the parent?
All childs and grandchilds (an so on..) nested to the parent.
Post edited, I think this is what you were asking for!
|
1

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);

https://3v4l.org/Fbet4

1 Comment

I'ts very close what I need. I realy thank you for your effort,... what i need for ex: Models.. need to show grandchildrens too. .. in other words.. everything under Models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.