Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:26:36+00:00 2026-06-05T06:26:36+00:00

So far, if I have to loop through a multidimensional array, I use a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:26:39+00:00Added an answer on June 5, 2026 at 6:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary with a key value pair and need to loop through
A book I have says this: a) Place each value of the one-dimensional array
I am trying to loop through an array and perform an httpwebrequest in each
i have a bunch of options in this select , each with value s
I'm trying to loop through a bunch of checkboxes i have created on my
I want to take a class, loop through it's properties, get the property value,
using chrome and ff so far have made a nasty glitch when using scrollTo
So far I have not been able to find an answer, but I wanted
so far I have done most view programming in code, but it really is
So far I have managed to write a query to list all the outgoing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.