PHP Iterate over Child's Child Efficiently

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have this code:



foreach($list as $id => $item_array) $is_admin) 
include('item.php');

$temp = $item_array;
while(!empty($temp['child']))
foreach($temp['child'] as $id => $item_array)
if(empty($temp['child']['child']))
unset($temp);





which functions but I'm going to need to duplicate the while block 5 times because my items can have up to six levels. There must be a more efficient way to do this, right? (The item.php just outputs HTML based on the array values)



In the simplest form $list would be:



Array
(
[id] => 1
[name] => Water
[date] => 2018-05-14 13:24:22
[content] => It's Good
[approved] => 1
[level] => 0
[child] => Array([0] => Array(
[id] => 2
[name] => Hydrogen
[date] => 2017-05-07 15:12:14
[content] => Part air
[approved] => 1
[level] => 1)
)
)


If needed I build $list with:



if($parent != -1) 
$list[$parent]['child'] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
else
$list[$id] = $this->build_list_array($id, $date, $content, $name, $approved, $level);

function build_comment_array($id, $date, $content, $name, $approved, $level)
return array('id' => $id,
'name' => $name,
'date' => $date,
'content' => $content,
'approved' => $approved,
'level' => $level);







share|improve this question

























    up vote
    1
    down vote

    favorite












    I have this code:



    foreach($list as $id => $item_array) $is_admin) 
    include('item.php');

    $temp = $item_array;
    while(!empty($temp['child']))
    foreach($temp['child'] as $id => $item_array)
    if(empty($temp['child']['child']))
    unset($temp);





    which functions but I'm going to need to duplicate the while block 5 times because my items can have up to six levels. There must be a more efficient way to do this, right? (The item.php just outputs HTML based on the array values)



    In the simplest form $list would be:



    Array
    (
    [id] => 1
    [name] => Water
    [date] => 2018-05-14 13:24:22
    [content] => It's Good
    [approved] => 1
    [level] => 0
    [child] => Array([0] => Array(
    [id] => 2
    [name] => Hydrogen
    [date] => 2017-05-07 15:12:14
    [content] => Part air
    [approved] => 1
    [level] => 1)
    )
    )


    If needed I build $list with:



    if($parent != -1) 
    $list[$parent]['child'] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
    else
    $list[$id] = $this->build_list_array($id, $date, $content, $name, $approved, $level);

    function build_comment_array($id, $date, $content, $name, $approved, $level)
    return array('id' => $id,
    'name' => $name,
    'date' => $date,
    'content' => $content,
    'approved' => $approved,
    'level' => $level);







    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have this code:



      foreach($list as $id => $item_array) $is_admin) 
      include('item.php');

      $temp = $item_array;
      while(!empty($temp['child']))
      foreach($temp['child'] as $id => $item_array)
      if(empty($temp['child']['child']))
      unset($temp);





      which functions but I'm going to need to duplicate the while block 5 times because my items can have up to six levels. There must be a more efficient way to do this, right? (The item.php just outputs HTML based on the array values)



      In the simplest form $list would be:



      Array
      (
      [id] => 1
      [name] => Water
      [date] => 2018-05-14 13:24:22
      [content] => It's Good
      [approved] => 1
      [level] => 0
      [child] => Array([0] => Array(
      [id] => 2
      [name] => Hydrogen
      [date] => 2017-05-07 15:12:14
      [content] => Part air
      [approved] => 1
      [level] => 1)
      )
      )


      If needed I build $list with:



      if($parent != -1) 
      $list[$parent]['child'] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
      else
      $list[$id] = $this->build_list_array($id, $date, $content, $name, $approved, $level);

      function build_comment_array($id, $date, $content, $name, $approved, $level)
      return array('id' => $id,
      'name' => $name,
      'date' => $date,
      'content' => $content,
      'approved' => $approved,
      'level' => $level);







      share|improve this question











      I have this code:



      foreach($list as $id => $item_array) $is_admin) 
      include('item.php');

      $temp = $item_array;
      while(!empty($temp['child']))
      foreach($temp['child'] as $id => $item_array)
      if(empty($temp['child']['child']))
      unset($temp);





      which functions but I'm going to need to duplicate the while block 5 times because my items can have up to six levels. There must be a more efficient way to do this, right? (The item.php just outputs HTML based on the array values)



      In the simplest form $list would be:



      Array
      (
      [id] => 1
      [name] => Water
      [date] => 2018-05-14 13:24:22
      [content] => It's Good
      [approved] => 1
      [level] => 0
      [child] => Array([0] => Array(
      [id] => 2
      [name] => Hydrogen
      [date] => 2017-05-07 15:12:14
      [content] => Part air
      [approved] => 1
      [level] => 1)
      )
      )


      If needed I build $list with:



      if($parent != -1) 
      $list[$parent]['child'] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
      else
      $list[$id] = $this->build_list_array($id, $date, $content, $name, $approved, $level);

      function build_comment_array($id, $date, $content, $name, $approved, $level)
      return array('id' => $id,
      'name' => $name,
      'date' => $date,
      'content' => $content,
      'approved' => $approved,
      'level' => $level);









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 14 at 20:22









      user3783243

      1083




      1083




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can do it recursively. Create a function (let's say, iterateChildren(array)) that iterates over the given array and when the children key is found you call iterateChildren(item['children']).



          This way you can have as many levels as you'd like, since the function calls itself at each level.



          Be cautious, though, because with too many levels you may run out of memory.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194394%2fphp-iterate-over-childs-child-efficiently%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            You can do it recursively. Create a function (let's say, iterateChildren(array)) that iterates over the given array and when the children key is found you call iterateChildren(item['children']).



            This way you can have as many levels as you'd like, since the function calls itself at each level.



            Be cautious, though, because with too many levels you may run out of memory.






            share|improve this answer



























              up vote
              4
              down vote



              accepted










              You can do it recursively. Create a function (let's say, iterateChildren(array)) that iterates over the given array and when the children key is found you call iterateChildren(item['children']).



              This way you can have as many levels as you'd like, since the function calls itself at each level.



              Be cautious, though, because with too many levels you may run out of memory.






              share|improve this answer

























                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                You can do it recursively. Create a function (let's say, iterateChildren(array)) that iterates over the given array and when the children key is found you call iterateChildren(item['children']).



                This way you can have as many levels as you'd like, since the function calls itself at each level.



                Be cautious, though, because with too many levels you may run out of memory.






                share|improve this answer















                You can do it recursively. Create a function (let's say, iterateChildren(array)) that iterates over the given array and when the children key is found you call iterateChildren(item['children']).



                This way you can have as many levels as you'd like, since the function calls itself at each level.



                Be cautious, though, because with too many levels you may run out of memory.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 14 at 22:51









                Sam Onela

                5,76961543




                5,76961543











                answered May 14 at 22:38









                Stefan Manciu

                1562




                1562






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194394%2fphp-iterate-over-childs-child-efficiently%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?