1. Computing

Discuss in my forum

How to use Arrays in PHP

By , About.com Guide

When you are working with PHP, one way to represent data is to use a variable. But sometimes a variable just isn't enough. Sometimes you need an array. An array is sort of like a variable that holds multiple entries. For example, if you wanted a variable called $state you could only hold one state name. But if you made the same variable an array, you could hold all 50 states.

First let's review how to set a variable in PHP

<?php

$state = "Wisconsin";

echo $state;

?>
If you run the code above it will simply return:

Wisconsin

Now, let's try that again as an array.

<?php

$states = array ('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware');

print_r ($states);

?>
If you run this you will see:

Array ( [0] => Alabama [1] => Alaska [2] => Arizona [3] => Arkansas [4] => California [5] => Colorado [6] => Connecticut [7] => Delaware )

As you can see, unlike a variable that only stores one value, you can list several values inside of the same array. We choose to list the first eight states (alphabetically) in our example. You will also notice that we used print_r instead of print or echo on our next line. Print_r is used to show the contents of an array. It is a very ugly way to actually display the contents of the array, but for testing and debugging it certainly does the trick.

All of the elements (the items in our array) are indexed by number. The first number is always 0. An array with 12 items in it, will always have it's last item as #11, because we start counting at 0 instead of 1. So, looking at our array, if you wanted to access Alabama you would use $states[0]. If you used $states[4] you would get California, the fifth item in our array (remember we start at 0.) Let's try it:

<?php

$states = array ('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware');

echo ($states[4]);

?>
Running this will display:

California

You can also setup keys to access each part of an array. So instead of having to refer to a state by it's number you can refer to it by it's key. Lets try setting up an array with a key. In this example the state will become the key, and the array will hold the capital cities.


<?php 

$states = array('Alabama' => 'Montgomery', 'Alaska' => 'Juneau', 'Arizona' => 'Phoenix', 'Arkansas' => 'Little Rock', 'California' => 'Sacramento', 'Colorado' => 'Denver', 'Connecticut' => 'Hartford', 'Delaware' => 'Dover'); 

print_r($states); 

?>
Here is another way to define the same array:

<?php 
$states['Alabama'] = 'Montgomery'; 
$states['Alaska'] = 'Juneau'; 
$states['Arizona'] = 'Phoenix'; 
$states['Arkansas'] = 'Little Rock'; 
$states['California'] = 'Sacramento'; 
$states['Colorado'] = 'Denver'; 
$states['Connecticut'] = 'Hartford'; 
$states['Delaware'] = 'Dover'; 
print_r ($states);
?> 
In both cases, if you run the code you will get this:

Array ( [Alabama] => Montgomery [Alaska] => Juneau [Arizona] => Phoenix [Arkansas] => Little Rock [California] => Sacramento [Colorado] => Denver [Connecticut] => Hartford [Delaware] => Dover )

Now let's access a state capital using the key of the state's name. We access this the same way we accessed it using a key that was a number. Let's try to access Dover, Delaware. Try this code:

<?php 

$states = array('Alabama' => 'Montgomery', 'Alaska' => 'Juneau', 'Arizona' => 'Phoenix', 'Arkansas' => 'Little Rock', 'California' => 'Sacramento', 'Colorado' => 'Denver', 'Connecticut' => 'Hartford', 'Delaware' => 'Dover'); 

echo ($states[Delaware]);

?> 
Running this code will simply give you:

Dover

REVIEW: What did we learn? We learned that arrays are sort of like a variable in that they hold data, however an array can hold many values when a variable can only hold one. We also learned that by default all items in an array are numbered (indexed) starting at zero. The numbers are called keys. We also learned that we can change the keys to be something more meaningful such as words.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. All About PHP Arrays

©2013 About.com. All rights reserved.