LAMP Tutorials
Lamp Tutorials Contact CrackFeed

Introduction to PHP multidimensional arrays

Part 4 of PHP Arrays Series

Authored on: July 14, 2011 by Jeffery Dilegge

Digg this tutorial!

Please support Crackfeed with a small donation.

Back to PHP Tutorials

Previous in series: Introduction to php associative arrays.


Today's article will be a PHP tutorial on using multidimensional arrays. Remember that an array is a variable that contains multiple variables that are organized by a KEY and a VALUE. The key is the "name" of the variable inside the array and the value is the variable's value. That is not the technical description, but I am trying to dummy it down a bit.

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Source: http://php.net/manual/en/language.types.array.php

Let's jump right in now, by looking at what a two dimensional array is and what a 3 dimensional array is, and how they would look in a practical data table. You can have more than 3 dimensions, but they are handled in the same manner as the 3 dimensional array, so I am not going into those.


2 dimensional array example

Employee Manifest

Bob Smith 12.20.1978 Male
John Doe 08.14.1943 Male
Lisa Flubber 02.26.1995 Female
Matt Turner 12.07.1978 Male
Kathy Cranky 02.24.1991 Female

3 dimensional array example

Employee Manifest

Warehouse employees
Bob Smith 12.20.1978 Male
John Doe 08.14.1943 Male
Lisa Flubber 02.26.1995 Female
Billing staff
Matt Turner 12.07.1978 Male
Kathy Cranky 02.24.1991 Female

As you can see, a 2 dimensional array will allow you to list each employee under the key "Employee Manifest". The 3 dimensional array does this too, but also allows you to split the employees into groups. Here is what the actual arrays look like. Note that I am using numeric arrays to create the multidimensional arrays, because I can easily define the names of the fields later when I actually put the arrays to use.


2 dimensional array code example

<?php
$manifest = array(
		array('Bob Smith', '12.20.1978', 'Male'),
		array('John Doe', '08.14.1943', 'Male'),
		array('Lisa Flubber', '02.26.1995', 'Female'),
		array('Matt Turner', '12.07.1978', 'Male'),
		array('Kathy Cranky', '02.24.1991', 'Female')
             );
?>


3 dimensional array code example

<?php
$manifest = array(
	array(
		array('Bob Smith', '12.20.1978', 'Male'),
		array('John Doe', '08.14.1943', 'Male'),
		array('Lisa Flubber', '02.26.1995', 'Female')
	),
	array(
		array('Matt Turner', '12.07.1978', 'Male'),
		array('Kathy Cranky', '02.24.1991', 'Female')
	)
);
?>

Now to use the data in a multidimensional array you can loop through it or you can explicity ask for a specific field from the array. Here is a simple way to grab a specific value from the array, in this case, I want the name of the second employee listed in the 3 dimensional arrray:

<?php
$manifest = array(
	array(
		array('Bob Smith', '12.20.1978', 'Male'),
		array('John Doe', '08.14.1943', 'Male'),
		array('Lisa Flubber', '02.26.1995', 'Female')
	),
	array(
		array('Matt Turner', '12.07.1978', 'Male'),
		array('Kathy Cranky', '02.24.1991', 'Female')
	)
);
echo $manifest[0][1][0];
?>

That code will display: John Doe


But if you want to loop though the 2 dimensional array, for the purpose of displaying a manifest in your browser for example, you would do something like this:

<?php

echo '<table cellSpacing="2" cellPadding="2" width="550" border="1">
	<tr>
		<td colspan="3">
			<h2>Employee Manifest</h2>
		</td>
	</tr>
';
for ($row = 0; $row < 3; $row++) {
	echo '
		<tr>
			<td>' . $manifest[0][$row1][0] . '</td>
			<td>' . $manifest[0][$row1][1] . '</td>
			<td>' . $manifest[0][$row1][2] . '</td>
		</tr>
	';
}
echo '
	</table>
';

?>

For each dimension that your PHP multidimensional array has, you will need one extra nested loop; at least when using numeric arrays. Associative arrays do not need so many loops, but I will get into that into my next lesson in the PHP array series. Here is an example of adding another nested loop for the same 3 dimensional array example I have been using throughout this tutorial.

<?php

echo '<table cellSpacing="2" cellPadding="2" width="550" border="1">
	<tr>
		<td colspan="3">
			<h2>Employee Manifest</h2>
		</td>
	</tr>
';
for ($row1 = 0; $row1 < 3; $row1++) {
	if ($row1 == 1) {
		echo '
			<tr>
				<td colspan="3">
					<strong>Warehouse employees</strong>
				</td>
			</tr>
		';
	} elseif ($row1 == 2) {
		echo '
			<tr>
				<td colspan="3">
					<strong>Billing Staff</strong>
				</td>
			</tr>
		';
	}
	for ($row2 = 0; $row2 < 3; $row2++) {
		echo '
			<tr>
				<td>' . $manifest[0][$row2][0] . '</td>
				<td>' . $manifest[0][$row2][1] . '</td>
				<td>' . $manifest[0][$row2][2] . '</td>
			</tr>
		';
	}
}
echo '
	</table>
';

?>

Coming July 15, 2011:   Next in series: Using multidimensional associative arrays



Other tutorials in this series:

First in series: Introduction to php arrays.
Second in series: Introduction to php numeric arrays.

Fourth in series: Introduction to php multidimensional arrays.

Links Directory | Jeffery Dilegge