Question:

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
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: A multidimensional array is an array whose elements are themselves arrays. To reach the inner values you must step down each level.

Step 2: The outer loop walks the top-level entries. For each entry, which is again an array, you run an inner loop over its elements. That means one loop nested inside another.

Step 3: A single loop only touches the top level, so it cannot read the inner values. Restricting to for only or while only is also wrong, since PHP allows any loop type here.

Step 4: The general and correct approach is nested foreach or for loops.

So the answer is option A.
Was this answer helpful?
0
0