2

I have the following data inside an multi level arrarray

1: id: 41, parent: null, name: lucas
2: id: 52, parent: null, name: george
3: id 98: parent: 41, name: julian
...

I need to loop through this array and a 'childrens' key and value to the parent while summing +1 to this value every time i run over an entry that has 'parent' not set to null. How to achieve this?

1: id: 41, parent: null, name: lucas, children: 1

I tried this inside a foreach

foreach($post as $parsedPost) {
    if($parsedPost['parent'] > 0){
        $idChild = $parsedPost['id'];
        $idParent = $parsedPost['parent'];
        $post[$idParent]["childrens"]++;
    }
 }

Of course, it returns a notice because the index ['childrens'] does not exist. Also, it's adding the childrens:value to the first children and not to the parent. Why?

5
  • From where this $idPai came? Commented Aug 1, 2016 at 5:27
  • Sorry @Saurabh, its supposed to be $idParent, i translated the var names before posting and forgot this one. EDIT: Fixed Commented Aug 1, 2016 at 5:43
  • to get rid of the notice add if( !isset($post[$idParent]['childrens']) ) { $post[$idParent]['childrens']=0; } before the line containing increment . Commented Aug 1, 2016 at 5:54
  • Thank you @olegsv. Do you know if it's possible to do what i want? I don't understand why my code does not work. If I echo $idChild has $idParent as a parent works, why $post[$idParent]["children"]++ does not work? Commented Aug 1, 2016 at 6:03
  • I wrote an implementation for this in 2010: stackoverflow.com/questions/3261228/… Commented Aug 1, 2016 at 6:12

1 Answer 1

1

Hope this helps.

$post[] = ["id" => "41", "parent" => null, "name" => "lucas"];
$post[] = ["id" => "52", "parent" => null, "name" => "george"];
$post[] = ["id" => "98", "parent" => "41", "name" => "Julian"];

foreach ($post as $parsedPost)
{
    if ($parsedPost['parent'] > 0 && $parsedPost['parent'] != "")
    {
        $idChild = $parsedPost['id'];
        $idParent = $parsedPost['parent'];
        $parentKey = array_search($idParent,array_column($post,'id'));
        if(!isset($post[$parentKey]["childrens"]))
        {
            $post[$parentKey]["childrens"] = 0;
        }
        $post[$parentKey]["childrens"]++;
    }
}
print_r($post);

Output:

Array
(
    [0] => Array
        (
            [id] => 41
            [parent] => 
            [name] => lucas
            [childrens] => 1
        )

    [1] => Array
        (
            [id] => 52
            [parent] => 
            [name] => george
        )

    [2] => Array
        (
            [id] => 98
            [parent] => 41
            [name] => Julian
        )

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

11 Comments

your code does work, thank you! But it's skipping 1 children for every entries that have a children. Do you know why?
I didn't get you can you explain in more detail?
Entry 1 on my database has 6 childrens and your script calculates 6 childrens. while Entry 2 on my database has 3 childrens and your script calculates 2 childrens. I am going nuts over this one :D Glad you are helping
I added 3 children for id 52 and it is showing me 3 children.
@LucasRabelloSimões can you post SQL table here?
|

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.