2 Dimensional arrays in PHP
In the post tables, there are 5 columns. When we use mysql_fetch_array() it will fetch back each column name, and it's value. You can store all of this information into an array of your own. But now, each position in the array will hold more than one value. It will hold a row of data from the database table. The new array will be like this:
$My_Array[ ] = array();
$My_Array[0] = 10;
$My_Array[1] = 20;
$My_Array[2] = 30;
So position 0 in the array above holds a value of 10, position 1 holds a value
of 20, and position 2 holds a value of 30.
But for us, we're returning a row of data from our table. Each row will be like this:

In the post tables, there are 5 columns. When we use mysql_fetch_array( ) it will fetch back each column name, and it's value. You can store all of this information into an array of your own. But now, each position in the array will hold more than one value. It will hold a row of data from the database table. The new array will be like this:
$My_Array[0] = "pos3", "mem1", "Clip Art", "Got some good clip art?", "2006-04-13 12:11:06"
So position 0 now holds more than one value. It holds 5 values, in the code above. This is a 2D array - an array where each postion holds more than one value. (You can also have a 3D array, but this is far more complex than we need.)
If you want to just access the datePosted value, you can do it like this:
$postData[0]['datePosted']
So the name of you 2D array goes first, followed by square brackets. In between the square brackets, you need a postion in the array. To access just a particular value in that postion, you type a column name (or key value). In the line above, we've specified the datePosted column.
If all that is a bit confusing, try this exercise.
Exercise
Add the following to the end of your for loop (the new line is in blue):
for ($i = 0; $i < $totalRows; ++$i) {
$postData[$i] = mysql_fetch_array($result);
}
print $postData[0]['threadTopic'] . "<BR>";
Refresh your page and see what happens. Now change the 0 to 1, save your work, and refresh the page. Now change 'threadTopic' to 'datePosted'. Again, reload the page. Try the other Column Names from the table above.
You can also add new keys and values to a 2D array. Simply type a new name in
between the square brackets, and its value. Like this:
$postData[0]['newValue'] = "new value here";
Exercise
Add this new for loop to your code (the new lines are in blue):
print $postData[0]['threadTopic'] . "<BR>";
for ($i = 0; $i < $totalRows; ++$i) {
$postData[$i]['newValue'] = $i;
}
print $postData[0]['newValue'] . "<BR>";
Save your work, refresh the page, and watch what happens. Now change the 0 to 1, save your work, and refresh the page. What did you notice?
In summary: use a 2D array when you want each position in your array to hold more than one value.
If you're still having problems grasping the concept of 2D arrays then think
of them like an Excel spreadsheet. Each row in the spreadsheet represents a
postion in the 2D array. Each column in the spreadsheet represents a value for
each postion. A normal array would look like this:

And a 2D array would look like this:

And now on with the forum code, which we'll continue in the next part. (Delete any code you added for the exercises above.)