I'm trying to rebuild a tree as a flat array. I successed in the function of attributing the parent_id to each element.
But now my issue is to store each element in a new array, which is outside of the function.
I've tried several ways (ps: the function is not defined in the same page as where it's called):
   function rebuildTreeAsFlatArray($source,$childrenPropertyName)          
   {     
       $myarray = array();
       foreach($source as $element)
       {
           if(isset($element["nodes"]))
           {
               $this->rebuildTreeAsFlatArray($element["nodes"],$childrenPropertyName);
           }
           array_push($myarray,$element);
       }
       return $myarray;
   }
$result = $arrayFunctionsTreeSaving->rebuildTreeAsFlatArray($tree,"nodes");
this gives me an array with only one element inside, the first one.
I've tried to use a global:
function rebuildTreeAsFlatArray($source,$childrenPropertyName)          
   {     
       global $myarray;
       foreach($source as $element)
       {
           if(isset($element["nodes"]))
           {
               $this->rebuildTreeAsFlatArray($element["nodes"],$childrenPropertyName);
           }
           array_push($myarray,$element);                     
       }
   }
   $myarray = array();
   $arrayFunctionsTreeSaving->rebuildTreeAsFlatArray($tree,"nodes");
and what's the reuslt: Warning: array_push() expects parameter 1 to be array, null given
Last but not least, but referenced parameter:
   function rebuildTreeAsFlatArray($source,$childrenPropertyName,array &$result)          
   {     
       foreach($source as $element)
       {
           if(isset($element[$childrenPropertyName]))
           {
               $this->rebuildTreeAsFlatArray($element[$childrenPropertyName],$childrenPropertyName,$result);
           }
           array_push($result,$element);                     
       }
   }
   $myarray = array();
   $arrayFunctionsTreeSaving->rebuildTreeAsFlatArray($tree,"nodes",$myarray);
and this gives me:
Catchable Fatal Error: Argument 3 passed to NRtworks\GlobalUtilsFunctionsBundle\Services\arrayFunctionsTreeSaving::rebuildTreeAsFlatArray() must be of the type array, integer given
now what should I do ? I'm lost
Here is a tree example:
array(10) {
    ["id"] = > int(6)["name"] = > string(9)"Subsidies" ["code"] = > string(6)"703000" ["sense"] = > string(2)"DR" ["lft"] = > int(11)["lvl"] = > int(2)["rgt"] = > int(20)["root"] = > int(1)["nodes"] = > array(2) {
        [0] = > array(10) {
            ["id"] = > int(29)["name"] = > string(13)"Subsidies USA" ["code"] = > string(6)"703200" ["sense"] = > string(2)"DR" ["lft"] = > int(12)["lvl"] = > int(3)["rgt"] = > int(13)["root"] = > int(1)["nodes"] = > array(0) {}
            ["$$hashKey"] = > string(3)"01Z"
        }
        [1] = > array(10) {
            ["id"] = > int(12)["name"] = > string(14)"Subventions FR" ["code"] = > string(6)"703100" ["sense"] = > string(2)"DR" ["lft"] = > int(14)["lvl"] = > int(3)["rgt"] = > int(19)["root"] = > int(1)["nodes"] = > array(0) {}
            ["$$hashKey"] = > string(3)"020"
        }
    }
    ["$$hashKey"] = > string(3)"00R"
}