0

I have a multidimensional array in Parent-Child format.

I want to add a new id in the array. The new id 'TTID' will be set in the order of Parent-Child. Meaning if ParentTTID is 1 then Child TTID should be 2, and its child should be 3.

So it's in order of Parent->Child->CHild. with unlimited levels of depth. Now the New Parent node id will start from last child's ID say 4 and its child will be 5 and so on

Array
(
    [0] =Array
        (
            [id] =0
            [value] =Total Income
            [parent] =-1
            [ttid] =1
            [children] =Array
                (
                    [0] =Array
                        (
                            [id] =2
                            [value] =Total Contributions
                            [parent] =0
                            [ttid] =0
                            [children] =Array
                                (
                                    [0] =Array
                                        (
                                            [id] =5
                                            [value] =Total Employers Contributions
                                            [parent] =2
                                            [ttid] =0
                                            [children] =Array
                                                (
                                                    [0] =Array
                                                        (
                                                            [id] =10
                                                            [value] =Employes unclassfied Contributions
                                                            [parent] =5
                                                            [ttid] =0
                                                        )

                                                )

                                        )

[1] =Array
    (
        [id] =1
        [value] =Total Expenses
        [parent] =-1
        [ttid] =0
        [children] =Array
            (
                [0] =Array
                    (
                        [id] =4
                        [value] =Total Benifit Payments
                        [parent] =1
                        [ttid] =0
                        [children] =Array
                            (
                                [0] =Array
                                    (
                                        [id] =9
                                        [value] =Directly to Participant
                                        [parent] =4
                                        [ttid] =0
                                        [children] =Array
                                            (
                                                [0] =Array
                                                    (
                                                        [id] =14
                                                        [value] =Some participant
                                                        [parent] =9
                                                        [ttid] =0
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

I tried to solve this using a method where I send 1 parent node Id at a time so that all its child will get marked. However, it doesn't seem to be working and it also gives me a warning: Cannot use a scalar value as an array in the line

function treetable_markRowChildren($elements,$parentId,$new_id)
{
  $i = 1; $arr=array();
    foreach ($elements as $element)
    {
        if ($element['parent'] == $parentId)
          $element['ttid'] = $new_id+$i; // The Error is shown in this line
          $i=$i+1;
          $new_id=$element['ttid'];

         if(isset($element['children']))
             {
             $parentId=$element['id'];
            $arr2= treetable_markRowChildren($element,$parentId,$new_id);
         }
        else
             break; //No CHildren

      $arr=$element;

    }

  return $arr;
}

I just want the TTID to be set in this sequential format. How do I achieve this? Thanks in advance.

Array
(
    [0] =Array
        (
            [id] =0
            [value] =Total Income
            [parent] =-1
            [ttid] =1
            [children] =Array
                (
                    [0] =Array
                        (
                            [id] =2
                            [value] =Total Contributions
                            [parent] =0
                            [ttid] =2
                            [children] =Array
                                (
                                    [0] =Array
                                        (
                                            [id] =5
                                            [value] =Total Employers Contributions
                                            [parent] =2
                                            [ttid] =3
                                            [children] =Array
                                                (
                                                    [0] =Array
                                                        (
                                                            [id] =10
                                                            [value] =Employes unclassfied Contributions
                                                            [parent] =5
                                                            [ttid] =4
                                                        )

                                                )

                                        )



    [1] =Array
        (
            [id] =1
            [value] =Total Expenses
            [parent] =-1
            [ttid] =4
            [children] =Array
                (
                    [0] =Array
                        (
                            [id] =4
                            [value] =Total Benifit Payments
                            [parent] =1
                            [ttid] =6
                            [children] =Array
                                (
                                    [0] =Array
                                        (
                                            [id] =9
                                            [value] =Directly to Participant
                                            [parent] =4
                                            [ttid] =7
                                            [children] =Array
                                                (
                                                    [0] =Array
                                                        (
                                                            [id] =14
                                                            [value] =Some participant
                                                            [parent] =9
                                                            [ttid] =8
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
Ankit Shah
  • 89
  • 2
  • 9
  • Do you mean something like this: [Is there a way to find out how “deep” a PHP array is?](https://stackoverflow.com/questions/262891/is-there-a-way-to-find-out-how-deep-a-php-array-is) – HTMHell May 04 '19 at 09:22
  • This code finds depth but how do I set the value. The value needs to increment as child are counted and that too recursively. It seems easy but its a brainer. Just try setting a value and you'll know it – Ankit Shah May 04 '19 at 09:35

0 Answers0