Dimensional Arrays:
The number of ‘dimensions’ refers to how many ‘levels’ or how ‘deep’ the array goes. Examples below:
[hr][hr][hr]
1-dimensional array:
This is the basic form of an array. In this example, we have 3 elements - the value of each is a string or an integer (they can be float,boolean etc - anything but an array).
Array
(
[0] => "first element's value"
[1] => 35
[2] => "third element's value"
)
[hr][hr][hr]
2-dimensional array:
A 2 dimensional array is basically just a regular 1 dimensional, standard array. The difference being, that instead of a string or integer as a value, it is another array (an array within an array - 2 levels of arrays - 2 dimensional).
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
[hr][hr][hr]
Access 1-dimensional array values (using a ‘for’ loop)
Below is using a for loop to get the values from a standard 1 dimensional array (with a fixed element count of 3).
[php]for($i=0;$i<3;$i++)
{
echo $array[$i]."
"; //Will access elements with a key of 0, then 1, then 2 in our $array variable. Then add a line break for readability.
}[/php]
You can see in the top block of code showing the 1-dimensional array, that our 3 elements have the keys 0, 1 and 2, therefore the code above would output the following:
first element's value
35
third element's value
If we were to use that same code on the second example (2-dimensional), the output would be the following:
Array
Array
Array
As the values of each element is an array, we do not have the values to hand.
[hr][hr][hr]
Accessing 2-dimensional array values (using nested ‘for’ loops)
As seen above, we can’t just echo the value of an array’s element if the value is actually an array itself. What did we do for the first array? We used a loop to iterate through it’s elements.
The values of the elements in the second array are also arrays. So we use the same technique:
[php]for($i=0;$i<3;$i++)
{
//echo $array[$i]."
"; //Doesn’t work on this array because the value is an array.
$inner_array = $array[$i]; //As we go through each element in the first level of the array, we set $inner_array to that value (in this 2-dimensional array, the value is an array, so we loop through it below)
for($k=0;$k<3;$k++) //We are using a different variable so as to not interfere with the outer loop
{
echo $inner_array[$k]; //outputs the elements’ values of the inner arrays
echo ‘
’;
echo $array[$i][$k]; //same as above but in the format of the loop in your original code (works the same because $inner_array = $array[$i])
echo ‘
’;
}
echo ‘
’;
}[/php]
The above when used with the second array will output:
[code]1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
------------------------[/code]
I may well have gone overboard. If anything I think I over-explained the simpler parts, and made less sense on the more complex part, but it’s surprisingly difficult to type an explanation it appears!
If this still hasn’t helped you to understand, my best advice would be to practice with some dummy arrays on a test PHP file, using 1-dimensional arrays to start off with (loop through outputting values). Then make it 2-dimensional and see if you can rework the loop to get it outputting all your values properly.
Good luck and let me know how you get on.