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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • From where this `$idPai` came? – Saurabh Aug 01 '16 at 05:27
  • Sorry @Saurabh, its supposed to be $idParent, i translated the var names before posting and forgot this one. EDIT: Fixed – Lucas Rabello Simões Aug 01 '16 at 05:43
  • to get rid of the notice add `if( !isset($post[$idParent]['childrens']) ) { $post[$idParent]['childrens']=0; }` before the line containing increment . – olegsv Aug 01 '16 at 05: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? – Lucas Rabello Simões Aug 01 '16 at 06:03
  • I wrote an implementation for this in 2010: http://stackoverflow.com/questions/3261228/convert-flat-array-to-the-multi-dimentional/3261351#3261351 – Bill Karwin Aug 01 '16 at 06:12

1 Answers1

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
        )

)
Saurabh
  • 776
  • 1
  • 5
  • 15
  • your code does work, thank you! But it's skipping 1 children for every entries that have a children. Do you know why? – Lucas Rabello Simões Aug 01 '16 at 06:27
  • I didn't get you can you explain in more detail? – Saurabh Aug 01 '16 at 06:30
  • `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 – Lucas Rabello Simões Aug 01 '16 at 06:38
  • I added 3 children for id 52 and it is showing me 3 children. – Saurabh Aug 01 '16 at 06:42
  • @LucasRabelloSimões can you post SQL table here? – olegsv Aug 01 '16 at 06:43
  • @olegsv and Saurabh Database: http://pastebin.com/gQymWdzW Code I had before asking here: http://pastebin.com/9dzzUG9b – Lucas Rabello Simões Aug 01 '16 at 06:50
  • I imported your sql and your code,after executing it I am getting count as 3 as expected http://i.stack.imgur.com/Evqvx.png – Saurabh Aug 01 '16 at 07:02
  • Your code is correct. I don't know what is going on but it's here. I reviewed my query and it was returning the incorrect amount of data when using PDO. And it gets even worse: mysqli returned the correct amount of data I was expecting. Thank you all for the help you provided, it was really helpful! – Lucas Rabello Simões Aug 01 '16 at 07:07
  • Note that the code is utterly slow. To make it more effective, at least move array_column to outside of foreach() loop. I would recommend to put the data into $post using id as a key, if that's possible in your app. – olegsv Aug 01 '16 at 07:10
  • @olegsv You are correct. If we can modify the `$post` as per parent is it would be very easy and fast – Saurabh Aug 01 '16 at 07:13
  • @olegsv and saurabh you are right, I'll try this. Thank you for the tips. I've never used PHP for something like this so I feel really confused. I am trying to mimic what Reddit does, but simpler. – Lucas Rabello Simões Aug 01 '16 at 10:41