cnicollonline

"I was just guessing at numbers and figures, pulling the puzzles apart"

A few weeks ago I had wrote Arrays and Loops with php [Level 1]. This topic is expanding on that idea. I’ll be covering more complex array examples using 2D multidimensional arrays.

Multidimensional Arrays

The idea of a multidimensional array is something that can easily confuse someone, especially when they become complex with information (ie. 2D and 3D arrays). I like to think of multidimensional arrays as a miniature database.

A multidimensional array can hold multiple values to a single key. Take a look at the output below.

Array
(
    [0] => Array
        (
            [name] => Cody
            [gender] => male
            [colour] => green
            [fruit] => apples
            [sport] => frisbee
        )

    [1] => Array
        (
            [name] => Sam
            [gender] => male
            [colour] => blue
            [fruit] => oranges
            [sport] => golf
        )

    [2] => Array
        (
            [name] => Jenny
            [gender] => female
            [colour] => pink
            [fruit] => pineapple
            [sport] => hockey
        )

)

This example is an example of a 2D array using key values. By default PHP will auto fill the keys, but this can lead to confusion down the road. To set up the array:

$people = array( array("name"=>"Cody",
						"gender"=>"male",
						"colour"=>"green",
						"fruit"=>"apples",
						"sport"=>"frisbee"),
				array("name"=>"Sam",
					"gender"=>"male",
						"colour"=>"blue",
						"fruit"=>"oranges",
						"sport"=>"golf"),
				array("name"=>"Jenny",
						"gender"=>"female",
						"colour"=>"pink",
						"fruit"=>"pineapple",
						"sport"=>"hockey")
				);

echo '<pre>';
	print_r($people);
echo '</pre>';

There’s a lot going on, take some time and read through it. First we set up a main container that will hold all of our information

$people

we then put a new array within our already created array.

Retrieving a single value is quite easy. We just call on what array element we want, then we ask for a specific property, in this example the name.

echo '<pre>';
echo $people[0]['name'];
echo '</pre>';

How this can be used

By looping of course! We can loop through this in many different ways to provide an example:

for ($i=0; $i<sizeOf($people); $i++) {
	$string = $people[$i]['name'] . "'s favorite fruit is " . $people[$i]['fruit'];
	if ($people[$i]['gender'] === "male") {
		$string .= " his favorite colour is " . $people[$i]['colour'] . " and he enjoys to play " . $people[$i]['sport'];
	}
	else if ($people[$i]['gender'] === "female") {
		$string .= " her favorite colour is " . $people[$i]['colour'] . " and she enjoys to play " . $people[$i]['sport'];
	}
	echo $string;
	echo '<br>';
}

output:

Cody's favorite fruit is apple his favorite colour is green and he enjoys to play frisbee
Sam's favorite fruit is oranges his favorite colour is blue and he enjoys to play golf
Jenny's favorite fruit is pineapple her favorite colour is pink and she enjoys to play hockey

Here I used a if statement within my for loop to check the gender value. If it were a male I had the output specify “he”, and the same check for a female.

Another use for a multidimensional array is a table layout, or grid.

$grid = array(array('Cody','male','green'),
			array('Sam','male','blue'),
			array('Jenny', 'female','pink')
);
for ($r=0; $r<sizeOf($grid); $r++) {
	for($c=0; $c<sizeOf($grid[$r]); $c++) {
		echo $grid[$r][$c]. " | ";
	}
	echo '<br>';
}

output:

Cody | male | green |
Sam | male | blue |
Jenny | female | pink |

Conclusion

multidimensional arrays make for an interesting topic, there is so much you can do, but without doing they can be hard to explain. Here is the source to help you get started.

<?php
$people = array( array("name"=>"Cody",
						"gender"=>"male",
						"colour"=>"green",
						"fruit"=>"apples",
						"sport"=>"frisbee"),
				array("name"=>"Sam",
					"gender"=>"male",
						"colour"=>"blue",
						"fruit"=>"oranges",
						"sport"=>"golf"),
				array("name"=>"Jenny",
						"gender"=>"female",
						"colour"=>"pink",
						"fruit"=>"pineapple",
						"sport"=>"hockey")
				);

echo '<pre>';
	print_r($people);
echo '</pre>';

// Get a single value
echo '<pre>';
echo $people[0]['name'];
echo '</pre>';
// ================================================= LOOP =================================================
for ($i=0; $i<sizeOf($people); $i++) {
	$string = $people[$i]['name'] . "'s favorite fruit is " . $people[$i]['fruit'];
	if ($people[$i]['gender'] === "male") {
		$string .= " his favorite colour is " . $people[$i]['colour'] . " and he enjoys to play " . $people[$i]['sport'];
	}
	else if ($people[$i]['gender'] === "female") {
		$string .= " her favorite colour is " . $people[$i]['colour'] . " and she enjoys to play " . $people[$i]['sport'];
	}
	echo $string;
	echo '<br>';
}

// ================================================= GRID =================================================
$grid = array(array('Cody','male','green'),
			array('Sam','male','blue'),
			array('Jenny', 'female','pink')
);
for ($r=0; $r<sizeOf($grid); $r++) {
	for($c=0; $c<sizeOf($grid[$r]); $c++) {
		echo $grid[$r][$c]. " | ";
	}
	echo '<br>';
}

?>

Write a Comment