Home arrow PHP arrow Page 5 - Arrays and PHP: A Primer

Arrays and PHP: A Primer

For many years, arrays have been used to logically store and sequence data. They have also been the point of confusion for many developers. In this article, Tuna will talk about the structure of arrays and how you can create and manipulate arrays with the PHP scripting language.

Author Info:
By: Tuna Celik
Rating: 4 stars4 stars4 stars4 stars4 stars / 30
December 20, 2001
  1. · Arrays and PHP: A Primer
  2. · The fundamentals of memory allocation
  3. · Arrays and their uses
  4. · Single dimension arrays
  5. · Multi-dimensional arrays
  6. · Conclusion

print this article

Arrays and PHP: A Primer - Multi-dimensional arrays
(Page 5 of 6 )

If you're just new to arrays, then chances are multi-dimensional arrays will be a bit hard to grasp. However, once you've worked your way through this page, they shouldn't seem that difficult anymore.

Multi-dimensional arrays are simply arrays that can contain another "dimension" of indexes. For example, instead of creating a singly indexed array, we could add another column of indexes, like this:



$Arthur[0][0] = "Index at 0,0";

$Arthur[0][1] = "Index at 0,1";

$Arthur[1][0] = "Index at 1,0";

$Arthur[1][1] = "Index at 1,1";


Our multi-dimensional array would be represented in our computers memory like this:

How our multi-dimensional array is represented in memory

A good example of using a multi-dimensional array is to store the details of several different people. In one index, we could store the person's name. In another, their age, and in another their phone number. We could then display their details in a loop, like this:


$People[0][1] = "John Doe";

$People[0][2] = 20;

$People[0][3] = "555-1465";

$People[1][1] = "Mary Moo";

$People[1][2] = 25;

$People[1][3] = "555-9007";

for($i = 0; $i < 2; $i++)


for($j = 0; $j < 4; $j++)

print $People[$i][$j] . "<br>";



Once again, our browser output would look like this:

The output from our multi-dimensional arrays loop

Let me explain the details of our new code. Firstly, we are creating a new two-dimensional array. We could create a three or even four-dimensional array if we liked.

Next, we set the values of each index in the array. The left index starts from 0 and goes until 1. The right index starts from 1 and goes to 3. Lastly, we use two for loops (one inside another) to access each element in the array. The first loop traverses through the left index, while the inner loop traverses through (and displays) the right index.

We can also create a multi-dimensional array using the array function you saw earlier. To create a two-dimensional array, we could use some code like this:


$action = array(

1 => array(

1 => "Yes"



print $action[1][1];


After this, the value of $action[1][1] would be equal to "Yes". You should experiment with arrays in PHP to become more familiar with them.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials