Home PHP PHP: Loop Multidimensional Arrays

Using foreach and for to Loop Through Multidimensional PHP Arrays

By a TechBitBytes Contributor, September 20, 2023
7 MIN READ |

If you are not familiar with PHP arrays, more so on creating, accessing, and modifying PHP arrays, we recommend that you first read A Beginner's Guide to Indexed and Associative Arrays. The tutorial is designed for PHP beginners who want to learn the basics of multidimensional arrays in PHP.

Simple arrays are used to hold values such as strings and numbers.

However, arrays can be used to hold other arrays. Multidimensional arrays are created when an array is assigned to another array.

A Simple Multidimensional Array

<?php

	$array_1 = array(1, 2, 3, 4, 5);
	$array_2 = array(6, 7, 8, 9, 10);
	$array_3 = array('a', 'b', 'c', 'd', 'e');
	
	$multi_array = array($array_1, $array_2, $array_3);
	
	echo $multi_array[1][2];
	
	/*
	Outputs 8
	*/
?>

In the example above, the line $multi_array[1][2] displays the value at row 1 column 3. $multi_array[0] holds $array_1 and $multi_array[1] holds $array_2. Further, $array_1[0] is 6, $array_1[1] is 7 and $array_1[2] is 8.

Therefore, echo $multi_array[1][2]; display 8.

The figure below can be used to show how multidimensional arrays are structured.

PHP multidimensional array illustration

Looping Through Multidimensional Arrays

Just like any simple arrays, you can loop through multidimensional arrays using either the for loop or foreach.

1. Using the for Loop

<?php
	
	$users = array(
				array('Jane', 23, 'Red'),
				array('Mark', 32, 'Blue'),
				array('Susan', 34, 'Green'),
				array('Peter', 29, 'Black')
			);

	for($i=0; $i<count($users); $i++){
		echo $users[$i][0] . 'is ' . $users[$i][1] . ' years old and loves the color ' . $users[$i][2] . '<br>';
	}
?>

When run, the above code will output:


	Jane is 23 years old and loves the color Red
	Mark is 32 years old and loves the color Blue
	Susan is 34 years old and loves the color Green
	Peter is 29 years old and loves the color Black

2. Using the foreach Loop (PHP Nested Loops)

To use the foreach loop, we need to use the PHP nested loop. This involves using the foreach loop within the for loop to access the individual array containing the keys and elements.

<?php
	
	$users = array(
				array('Jane'=>'PHP'),
				array('Mark'=>'JavaScript'),
				array('Susan'=>'Python'),
				array('Peter'=>'Java')
			);
			
	for($i=0; $i < count($users); $i++){
		$sec_array = $users[$i]; //store individual arrays within the $users array
		foreach($sec_array as $prsn=>$value){
			echo $prsn . '\'s favorite programming language is ' . $value . '<br>';
		}
	}
?>

	Jane's favorite programming language is PHP
	Mark's favorite programming language is JavaScript
	Susan's favorite programming language is Python
	Peter's favorite programming language is Java

 

  This article is written to the best of the author's knowledge. TechBitBytes(TBB) ensures that all articles are constantly updated with the latest information.