Multidimensional Arrays in PHP
PHP/MySQL
Last Updated: 2007-04-22 14:58:19
Last Updated: 2007-04-22 14:58:19
STEP 1: Populating a Multidimensional Array in PHP
In PHP a multidimensional array is an array inside of an array. So to populate it you simply create an array where you would define a value in a single dimension array.
<?php
// this example defines a single dimensional array
$singleArray = array("value1", "value2","value3");
// this example populates a multidimensional array
// with a display name and a file name
$multidimensionalArray = array(
array("Vacation 2004", "vaca2004_01.jpg"),
array("Christmas 2004", "xmas04_01.jpg"),
array("Easter 2006", "east06_12.jpg"),
array("Birthday 2007", "brth07_08.jpg")
);
?>
As you can see from the two examples above where in a single dimensional array you would place a value in a multidimensional array you place an array with additional values.
<?php
// this example defines a single dimensional array
$singleArray = array("value1", "value2","value3");
// this example populates a multidimensional array
// with a display name and a file name
$multidimensionalArray = array(
array("Vacation 2004", "vaca2004_01.jpg"),
array("Christmas 2004", "xmas04_01.jpg"),
array("Easter 2006", "east06_12.jpg"),
array("Birthday 2007", "brth07_08.jpg")
);
?>
As you can see from the two examples above where in a single dimensional array you would place a value in a multidimensional array you place an array with additional values.
STEP 2: The ForEach Loop
Now that we've populated the array we need a way to access it's data. To do this we use a loop. PHP has a special loop that is intended especially for arrays. This loop is called a "ForEach" loop. This loop will loop through each element in an array.
<?php
// this example defines a single dimensional array
$singleArray = array("value1", "value2","value3");
foreach ($singleArray as $a) { // loop through the array
print $a . "<br>";
}
?>
In this example we created an alias name for our "singleArray" array variable; the alias "a". "$singleArray as $a". Then the ForEach loop, loops through the array. This example would output:
value1
value2
value3
<?php
// this example defines a single dimensional array
$singleArray = array("value1", "value2","value3");
foreach ($singleArray as $a) { // loop through the array
print $a . "<br>";
}
?>
In this example we created an alias name for our "singleArray" array variable; the alias "a". "$singleArray as $a". Then the ForEach loop, loops through the array. This example would output:
value1
value2
value3
STEP 3: Looping through the Multidimensional Array
So how do we get to the second dimension? Well if a multidimensional array is an array inside of an array, then to get to it's data we must use a loop inside of a loop.
The outer loop will iterate through each element in the outer array, the inner loop will iterate through the array contained in the parent arrays element.
<?php
// this example populates a multidimensional array
// with a display name and a file name
$multidimensionalArray = array(
array("vaca2004_01.jpg", "Vacation 2004"),
array("xmas04_01.jpg", "Christmas 2004"),
array("east06_12.jpg", "Easter 2006"),
array("brth07_08.jpg", "Birthday 2007")
);
// the outer loop
foreach ($multidimensionalArray as $a) { // loop through the array
// the inner loop
$flip=0;
foreach ($a as $b) { // loop through the inner elements
IF($flip == 0){
print <a href=\"images/$b\">";
$flip++;
}
ELSE{
print $b . </a><br>";
$flip = 0;
}
}
}
?>
Again, we created an alias in the inner loop called "b". The inner ForEach loop loops through each element inside of each child array.
The outer loop will iterate through each element in the outer array, the inner loop will iterate through the array contained in the parent arrays element.
<?php
// this example populates a multidimensional array
// with a display name and a file name
$multidimensionalArray = array(
array("vaca2004_01.jpg", "Vacation 2004"),
array("xmas04_01.jpg", "Christmas 2004"),
array("east06_12.jpg", "Easter 2006"),
array("brth07_08.jpg", "Birthday 2007")
);
// the outer loop
foreach ($multidimensionalArray as $a) { // loop through the array
// the inner loop
$flip=0;
foreach ($a as $b) { // loop through the inner elements
IF($flip == 0){
print <a href=\"images/$b\">";
$flip++;
}
ELSE{
print $b . </a><br>";
$flip = 0;
}
}
}
?>
Again, we created an alias in the inner loop called "b". The inner ForEach loop loops through each element inside of each child array.
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
