Question:easy

How do you loop through a multidimensional array in PHP?

Show Hint

Each extra array dimension needs one more loop wrapped inside the previous one.
Updated On: Jul 2, 2026
  • Using nested foreach or for loops
  • Using a single foreach loop
  • Using a for loop only
  • Using the while loop only
Show Solution

The Correct Option is A

Solution and Explanation

Idea: Count how many index levels the data has.

A 2D PHP array needs two index accesses, say $arr[i][j]$. Each independent index needs its own loop, so a depth of 2 needs 2 nested loops.

A single loop gives only $arr[i]$, which is still an array, not a scalar. The claim that only for or only while works is false, because foreach, for and while can all nest.

General rule for depth $d$:
\[ \text{loops needed} = d \]
For a multidimensional array this means nested loops.
\[\boxed{\text{Option A: nested foreach or for loops}}\]
Was this answer helpful?
0