Is there a way to loop through a multidimensional array without knowing it's depth?












9















So far, if I have to loop through a multidimensional array, I use a foreach loop for each dimension.



e.g for two dimensions



foreach($array as $key=>$value)
{
foreach($value as $k2=>$v2)
{
echo
}
}


What do I do when I don't know the depth of the array? ie the depth is variable.



The only thing I can think of is to code a whole stack of loops and to break the loop if the next value is not an array.This seems a little silly.



Is there a better way?










share|improve this question



























    9















    So far, if I have to loop through a multidimensional array, I use a foreach loop for each dimension.



    e.g for two dimensions



    foreach($array as $key=>$value)
    {
    foreach($value as $k2=>$v2)
    {
    echo
    }
    }


    What do I do when I don't know the depth of the array? ie the depth is variable.



    The only thing I can think of is to code a whole stack of loops and to break the loop if the next value is not an array.This seems a little silly.



    Is there a better way?










    share|improve this question

























      9












      9








      9


      4






      So far, if I have to loop through a multidimensional array, I use a foreach loop for each dimension.



      e.g for two dimensions



      foreach($array as $key=>$value)
      {
      foreach($value as $k2=>$v2)
      {
      echo
      }
      }


      What do I do when I don't know the depth of the array? ie the depth is variable.



      The only thing I can think of is to code a whole stack of loops and to break the loop if the next value is not an array.This seems a little silly.



      Is there a better way?










      share|improve this question














      So far, if I have to loop through a multidimensional array, I use a foreach loop for each dimension.



      e.g for two dimensions



      foreach($array as $key=>$value)
      {
      foreach($value as $k2=>$v2)
      {
      echo
      }
      }


      What do I do when I don't know the depth of the array? ie the depth is variable.



      The only thing I can think of is to code a whole stack of loops and to break the loop if the next value is not an array.This seems a little silly.



      Is there a better way?







      php arrays nested-loops






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 7 '12 at 9:19









      Matthew AndrianakosMatthew Andrianakos

      95116




      95116
























          5 Answers
          5






          active

          oldest

          votes


















          18














          Yes, you can use recursion. Here's an example where you output all the elements in an array:



          function printAll($a) {
          if (!is_array($a)) {
          echo $a, ' ';
          return;
          }

          foreach($a as $v) {
          printAll($v);
          }
          }

          $array = array('hello',
          array('world',
          '!',
          array('whats'),
          'up'),
          array('?'));
          printAll($array);


          What you should always remember when doing recursion is that you need a base case where you won't go any deeper.



          I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.



          The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.






          share|improve this answer





















          • 1





            No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

            – Ярослав Рахматуллин
            Dec 1 '17 at 12:38











          • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

            – Matt Smith
            Jul 18 '18 at 5:15



















          1














          You can use recursion for this problem:



          Here is one example



          $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value"))));

          //print_r($array);

          printAllValues($array);

          function printAllValues($arr) {
          if(!is_array($arr)) {
          echo '<br />' . $arr;
          return;
          }
          foreach($arr as $k => $v) {
          printAllValues($v);
          }
          }


          It will use recursion to loop through array



          It will print like



          a
          b
          c
          final value





          share|improve this answer































            0














            Simple function inside array_walk_recursive to show the level of nesting and the keys and values:



            array_walk_recursive($array, function($v, $k) {
            static $l = 0;
            echo "Level " . $l++ . ": $k => $vn";
            });


            Another one showing use with a reference to get a result:



            array_walk_recursive($array, function($v) use(&$result) {
            $result = $v;
            });





            share|improve this answer
























            • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

              – Matt Smith
              Jul 18 '18 at 5:17





















            0














            You can do the below function for loop-through-a-multidimensional-array-without-knowing-its-depth



            // recursive function loop through the dimensional array
            function loop($array){

            //loop each row of array
            foreach($array as $key => $value)
            {
            //if the value is array, it will do the recursive
            if(is_array($value) ) $array[$key] = loop($array[$key]);

            if(!is_array($value))
            {
            // you can do your algorithm here
            // example:
            $array[$key] = (string) $value; // cast value to string data type

            }
            }

            return $array;
            }


            by using above function, it will go through each of the multi dimensional array, below is the sample array you could pass to loop function :



             //array sample to pass to loop() function
            $data = [
            'invoice' => [
            'bill_information' => [
            'price' => 200.00,
            'quantity' => 5
            ],
            'price_per_quantity' => 50.00
            ],
            'user_id' => 20
            ];

            // then you can pass it like this :
            $result = loop($data);
            var_dump($result);

            //it will convert all the value to string for this example purpose





            share|improve this answer

































              0














              Based on previous recursion examples, here is a function that keeps an array of the path of keys a value is under, in case you need to know how you got there:



              function recurse($a,$keys=array()) 
              {
              if (!is_array($a))
              {
              echo implode("-", $keys)." => $a <br>";
              return;
              }
              foreach($a as $k=>$v)
              {
              $newkeys = array_merge($keys,array($k));
              recurse($v,$newkeys);
              }
              }


              recurse($array);





              share|improve this answer























                Your Answer






                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: "1"
                };
                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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10928993%2fis-there-a-way-to-loop-through-a-multidimensional-array-without-knowing-its-dep%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                18














                Yes, you can use recursion. Here's an example where you output all the elements in an array:



                function printAll($a) {
                if (!is_array($a)) {
                echo $a, ' ';
                return;
                }

                foreach($a as $v) {
                printAll($v);
                }
                }

                $array = array('hello',
                array('world',
                '!',
                array('whats'),
                'up'),
                array('?'));
                printAll($array);


                What you should always remember when doing recursion is that you need a base case where you won't go any deeper.



                I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.



                The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.






                share|improve this answer





















                • 1





                  No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                  – Ярослав Рахматуллин
                  Dec 1 '17 at 12:38











                • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                  – Matt Smith
                  Jul 18 '18 at 5:15
















                18














                Yes, you can use recursion. Here's an example where you output all the elements in an array:



                function printAll($a) {
                if (!is_array($a)) {
                echo $a, ' ';
                return;
                }

                foreach($a as $v) {
                printAll($v);
                }
                }

                $array = array('hello',
                array('world',
                '!',
                array('whats'),
                'up'),
                array('?'));
                printAll($array);


                What you should always remember when doing recursion is that you need a base case where you won't go any deeper.



                I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.



                The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.






                share|improve this answer





















                • 1





                  No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                  – Ярослав Рахматуллин
                  Dec 1 '17 at 12:38











                • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                  – Matt Smith
                  Jul 18 '18 at 5:15














                18












                18








                18







                Yes, you can use recursion. Here's an example where you output all the elements in an array:



                function printAll($a) {
                if (!is_array($a)) {
                echo $a, ' ';
                return;
                }

                foreach($a as $v) {
                printAll($v);
                }
                }

                $array = array('hello',
                array('world',
                '!',
                array('whats'),
                'up'),
                array('?'));
                printAll($array);


                What you should always remember when doing recursion is that you need a base case where you won't go any deeper.



                I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.



                The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.






                share|improve this answer















                Yes, you can use recursion. Here's an example where you output all the elements in an array:



                function printAll($a) {
                if (!is_array($a)) {
                echo $a, ' ';
                return;
                }

                foreach($a as $v) {
                printAll($v);
                }
                }

                $array = array('hello',
                array('world',
                '!',
                array('whats'),
                'up'),
                array('?'));
                printAll($array);


                What you should always remember when doing recursion is that you need a base case where you won't go any deeper.



                I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.



                The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 7 '12 at 10:10

























                answered Jun 7 '12 at 9:28









                Emil VikströmEmil Vikström

                74.5k13109151




                74.5k13109151








                • 1





                  No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                  – Ярослав Рахматуллин
                  Dec 1 '17 at 12:38











                • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                  – Matt Smith
                  Jul 18 '18 at 5:15














                • 1





                  No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                  – Ярослав Рахматуллин
                  Dec 1 '17 at 12:38











                • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                  – Matt Smith
                  Jul 18 '18 at 5:15








                1




                1





                No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                – Ярослав Рахматуллин
                Dec 1 '17 at 12:38





                No. Use the builtin array_walk_recursive(). PHP sucks at recursion.

                – Ярослав Рахматуллин
                Dec 1 '17 at 12:38













                array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                – Matt Smith
                Jul 18 '18 at 5:15





                array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                – Matt Smith
                Jul 18 '18 at 5:15













                1














                You can use recursion for this problem:



                Here is one example



                $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value"))));

                //print_r($array);

                printAllValues($array);

                function printAllValues($arr) {
                if(!is_array($arr)) {
                echo '<br />' . $arr;
                return;
                }
                foreach($arr as $k => $v) {
                printAllValues($v);
                }
                }


                It will use recursion to loop through array



                It will print like



                a
                b
                c
                final value





                share|improve this answer




























                  1














                  You can use recursion for this problem:



                  Here is one example



                  $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value"))));

                  //print_r($array);

                  printAllValues($array);

                  function printAllValues($arr) {
                  if(!is_array($arr)) {
                  echo '<br />' . $arr;
                  return;
                  }
                  foreach($arr as $k => $v) {
                  printAllValues($v);
                  }
                  }


                  It will use recursion to loop through array



                  It will print like



                  a
                  b
                  c
                  final value





                  share|improve this answer


























                    1












                    1








                    1







                    You can use recursion for this problem:



                    Here is one example



                    $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value"))));

                    //print_r($array);

                    printAllValues($array);

                    function printAllValues($arr) {
                    if(!is_array($arr)) {
                    echo '<br />' . $arr;
                    return;
                    }
                    foreach($arr as $k => $v) {
                    printAllValues($v);
                    }
                    }


                    It will use recursion to loop through array



                    It will print like



                    a
                    b
                    c
                    final value





                    share|improve this answer













                    You can use recursion for this problem:



                    Here is one example



                    $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value"))));

                    //print_r($array);

                    printAllValues($array);

                    function printAllValues($arr) {
                    if(!is_array($arr)) {
                    echo '<br />' . $arr;
                    return;
                    }
                    foreach($arr as $k => $v) {
                    printAllValues($v);
                    }
                    }


                    It will use recursion to loop through array



                    It will print like



                    a
                    b
                    c
                    final value






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 7 '12 at 9:28









                    SanjaySanjay

                    1,25011027




                    1,25011027























                        0














                        Simple function inside array_walk_recursive to show the level of nesting and the keys and values:



                        array_walk_recursive($array, function($v, $k) {
                        static $l = 0;
                        echo "Level " . $l++ . ": $k => $vn";
                        });


                        Another one showing use with a reference to get a result:



                        array_walk_recursive($array, function($v) use(&$result) {
                        $result = $v;
                        });





                        share|improve this answer
























                        • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                          – Matt Smith
                          Jul 18 '18 at 5:17


















                        0














                        Simple function inside array_walk_recursive to show the level of nesting and the keys and values:



                        array_walk_recursive($array, function($v, $k) {
                        static $l = 0;
                        echo "Level " . $l++ . ": $k => $vn";
                        });


                        Another one showing use with a reference to get a result:



                        array_walk_recursive($array, function($v) use(&$result) {
                        $result = $v;
                        });





                        share|improve this answer
























                        • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                          – Matt Smith
                          Jul 18 '18 at 5:17
















                        0












                        0








                        0







                        Simple function inside array_walk_recursive to show the level of nesting and the keys and values:



                        array_walk_recursive($array, function($v, $k) {
                        static $l = 0;
                        echo "Level " . $l++ . ": $k => $vn";
                        });


                        Another one showing use with a reference to get a result:



                        array_walk_recursive($array, function($v) use(&$result) {
                        $result = $v;
                        });





                        share|improve this answer













                        Simple function inside array_walk_recursive to show the level of nesting and the keys and values:



                        array_walk_recursive($array, function($v, $k) {
                        static $l = 0;
                        echo "Level " . $l++ . ": $k => $vn";
                        });


                        Another one showing use with a reference to get a result:



                        array_walk_recursive($array, function($v) use(&$result) {
                        $result = $v;
                        });






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 24 '18 at 21:35









                        AbraCadaverAbraCadaver

                        58.1k73966




                        58.1k73966













                        • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                          – Matt Smith
                          Jul 18 '18 at 5:17





















                        • array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                          – Matt Smith
                          Jul 18 '18 at 5:17



















                        array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                        – Matt Smith
                        Jul 18 '18 at 5:17







                        array_walk_recursive only handles leaf nodes and skips sub-arrays. It would be unusable with the example given. php.net/manual/en/function.array-walk-recursive.php

                        – Matt Smith
                        Jul 18 '18 at 5:17













                        0














                        You can do the below function for loop-through-a-multidimensional-array-without-knowing-its-depth



                        // recursive function loop through the dimensional array
                        function loop($array){

                        //loop each row of array
                        foreach($array as $key => $value)
                        {
                        //if the value is array, it will do the recursive
                        if(is_array($value) ) $array[$key] = loop($array[$key]);

                        if(!is_array($value))
                        {
                        // you can do your algorithm here
                        // example:
                        $array[$key] = (string) $value; // cast value to string data type

                        }
                        }

                        return $array;
                        }


                        by using above function, it will go through each of the multi dimensional array, below is the sample array you could pass to loop function :



                         //array sample to pass to loop() function
                        $data = [
                        'invoice' => [
                        'bill_information' => [
                        'price' => 200.00,
                        'quantity' => 5
                        ],
                        'price_per_quantity' => 50.00
                        ],
                        'user_id' => 20
                        ];

                        // then you can pass it like this :
                        $result = loop($data);
                        var_dump($result);

                        //it will convert all the value to string for this example purpose





                        share|improve this answer






























                          0














                          You can do the below function for loop-through-a-multidimensional-array-without-knowing-its-depth



                          // recursive function loop through the dimensional array
                          function loop($array){

                          //loop each row of array
                          foreach($array as $key => $value)
                          {
                          //if the value is array, it will do the recursive
                          if(is_array($value) ) $array[$key] = loop($array[$key]);

                          if(!is_array($value))
                          {
                          // you can do your algorithm here
                          // example:
                          $array[$key] = (string) $value; // cast value to string data type

                          }
                          }

                          return $array;
                          }


                          by using above function, it will go through each of the multi dimensional array, below is the sample array you could pass to loop function :



                           //array sample to pass to loop() function
                          $data = [
                          'invoice' => [
                          'bill_information' => [
                          'price' => 200.00,
                          'quantity' => 5
                          ],
                          'price_per_quantity' => 50.00
                          ],
                          'user_id' => 20
                          ];

                          // then you can pass it like this :
                          $result = loop($data);
                          var_dump($result);

                          //it will convert all the value to string for this example purpose





                          share|improve this answer




























                            0












                            0








                            0







                            You can do the below function for loop-through-a-multidimensional-array-without-knowing-its-depth



                            // recursive function loop through the dimensional array
                            function loop($array){

                            //loop each row of array
                            foreach($array as $key => $value)
                            {
                            //if the value is array, it will do the recursive
                            if(is_array($value) ) $array[$key] = loop($array[$key]);

                            if(!is_array($value))
                            {
                            // you can do your algorithm here
                            // example:
                            $array[$key] = (string) $value; // cast value to string data type

                            }
                            }

                            return $array;
                            }


                            by using above function, it will go through each of the multi dimensional array, below is the sample array you could pass to loop function :



                             //array sample to pass to loop() function
                            $data = [
                            'invoice' => [
                            'bill_information' => [
                            'price' => 200.00,
                            'quantity' => 5
                            ],
                            'price_per_quantity' => 50.00
                            ],
                            'user_id' => 20
                            ];

                            // then you can pass it like this :
                            $result = loop($data);
                            var_dump($result);

                            //it will convert all the value to string for this example purpose





                            share|improve this answer















                            You can do the below function for loop-through-a-multidimensional-array-without-knowing-its-depth



                            // recursive function loop through the dimensional array
                            function loop($array){

                            //loop each row of array
                            foreach($array as $key => $value)
                            {
                            //if the value is array, it will do the recursive
                            if(is_array($value) ) $array[$key] = loop($array[$key]);

                            if(!is_array($value))
                            {
                            // you can do your algorithm here
                            // example:
                            $array[$key] = (string) $value; // cast value to string data type

                            }
                            }

                            return $array;
                            }


                            by using above function, it will go through each of the multi dimensional array, below is the sample array you could pass to loop function :



                             //array sample to pass to loop() function
                            $data = [
                            'invoice' => [
                            'bill_information' => [
                            'price' => 200.00,
                            'quantity' => 5
                            ],
                            'price_per_quantity' => 50.00
                            ],
                            'user_id' => 20
                            ];

                            // then you can pass it like this :
                            $result = loop($data);
                            var_dump($result);

                            //it will convert all the value to string for this example purpose






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 12 '18 at 5:03

























                            answered Dec 12 '18 at 4:46









                            bathulah mahirbathulah mahir

                            342315




                            342315























                                0














                                Based on previous recursion examples, here is a function that keeps an array of the path of keys a value is under, in case you need to know how you got there:



                                function recurse($a,$keys=array()) 
                                {
                                if (!is_array($a))
                                {
                                echo implode("-", $keys)." => $a <br>";
                                return;
                                }
                                foreach($a as $k=>$v)
                                {
                                $newkeys = array_merge($keys,array($k));
                                recurse($v,$newkeys);
                                }
                                }


                                recurse($array);





                                share|improve this answer




























                                  0














                                  Based on previous recursion examples, here is a function that keeps an array of the path of keys a value is under, in case you need to know how you got there:



                                  function recurse($a,$keys=array()) 
                                  {
                                  if (!is_array($a))
                                  {
                                  echo implode("-", $keys)." => $a <br>";
                                  return;
                                  }
                                  foreach($a as $k=>$v)
                                  {
                                  $newkeys = array_merge($keys,array($k));
                                  recurse($v,$newkeys);
                                  }
                                  }


                                  recurse($array);





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Based on previous recursion examples, here is a function that keeps an array of the path of keys a value is under, in case you need to know how you got there:



                                    function recurse($a,$keys=array()) 
                                    {
                                    if (!is_array($a))
                                    {
                                    echo implode("-", $keys)." => $a <br>";
                                    return;
                                    }
                                    foreach($a as $k=>$v)
                                    {
                                    $newkeys = array_merge($keys,array($k));
                                    recurse($v,$newkeys);
                                    }
                                    }


                                    recurse($array);





                                    share|improve this answer













                                    Based on previous recursion examples, here is a function that keeps an array of the path of keys a value is under, in case you need to know how you got there:



                                    function recurse($a,$keys=array()) 
                                    {
                                    if (!is_array($a))
                                    {
                                    echo implode("-", $keys)." => $a <br>";
                                    return;
                                    }
                                    foreach($a as $k=>$v)
                                    {
                                    $newkeys = array_merge($keys,array($k));
                                    recurse($v,$newkeys);
                                    }
                                    }


                                    recurse($array);






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 1 at 19:25









                                    HenryHenry

                                    1,04311219




                                    1,04311219






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10928993%2fis-there-a-way-to-loop-through-a-multidimensional-array-without-knowing-its-dep%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                        SQL update select statement

                                        'app-layout' is not a known element: how to share Component with different Modules