Two Dimensional Arrays

Hello,

I’d like to learn how the following code for two dimensional arrays works:

[php]

For loop test <?php // Declare two-dimensional array $matrix = array (array (1,2,3), array (4,5,6), array (7,8,9) ); echo "

"."This is two dimensional array"."

".""; // Print two dimensional array element for($row = 0 ; $row <3 ; $row++) { for($col = 0 ;$col <3 ; $col++) { echo $matrix[$row][$col]."\t"."\t"; } echo ""; } ?> [/php]

How does the code determine which is $row and $col when it was not assigned or mentioned earlier in the code> Are $row for ROW and $col for COLUMNS standard php variables?

If you have a better way of explaining this other than my train of thought (also how it relates to FOR loop) please do so.

Thanks,
Chris

The variables $row and $col are simply for readability. It may help you to understand by seeing the layout of that array with the keys included:

[code]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
    )

)[/code]

The first for loop is going from 0 - 2 (the first level of the array) and in each one, looping from element 0 - 2 again (second level). The first level acts as the rows, with each row containing 3 columns.

That explanation may well have been horrific, if this hasn’t helped let me know and I’ll try to find a better way of explaining!

Hello Smokey PHP,

Thanks for the reply.

I’ve tried to wrap my head around it but, unfortunately, am still confused.

Pardon my ignorance, but perhaps I should start with a basic question: What makes it a two dimensional array? Which is the first, which is the second dimension?

Why two sets of FOR-loops?

[php]for($row = 0 ; $row <3 ; $row++) //Which values does this loop of $row represent? //
{
for($col = 0 ;$col <3 ; $col++)//Which values does this loop of $row represent?//
{
echo $matrix[$row][$col]."\t"."\t"; //What is echoed by $row and $col?//[/php]

How or in what sequence does the program process the code (i.e. first, the FOR-loop of $row then $col OR both FOR-loops --$row and $col-- simultaneously).

I hope my questions are making sense. Again, pardon the ignorance; this is my first experience with any type programming.

Thanks again—and in advance–to Smokey PHP and to anyone who may have the explanation.

Chris

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.

WOW! What an extensive explanation.

Sorry, I just saw this; thought email alerts were always sent for responses. Good thing I checked.

I haven’t gone through your explanation yet, SMOKEY PHP, but just want to thank you already.

I’ll seriously delve into it.

Whether I eventually grasp it or not, your response is truly and greatly appreciated.

Thanks again, SMOKEY PHP!
Chris

Sponsor our Newsletter | Privacy Policy | Terms of Service