By Ligaya Turmelle
What exactly are arrays anyway? What's the big deal?
Arrays a basic structure in most programming languages (including PHP) that holds data. The big deal is that it lets you save as many pieces of data as you want (1 - n) all in 1 structure that is easy to work with and access. Might sound hard but it really isn't. Lets take a walk down Array Street.
Think of an array as a city street with houses on it.
Lets pretend the array is the street. On this street you have houses with addresses (100 - 105). And living in those houses you have families ( the Smiths, Browns, Thoms, Jones's, Kings and the Brookes). So to talk to the Jones Family you would have to go to Array Street house number 103. How does this translate into code?
$street = array(); // Make Array Street $street[100] = 'Smith'; // Smith family lives at 100 Array Street $street[101] = 'Brown'; // Brown family lives at 101 Array Street $street[102] = 'Thom'; // Thacher family lives at 102 Array Street $street[103] = 'Jones'; // Jones family lives at 103 Array Street $street[104] = 'King'; // King family lives at 104 Array Street $street[105] = 'Brooke'; // Brookes family lives at 105 Array Street
So the house address is the key to the array and the means by which we can find the families that live at that address. Make sense? To read PHP's explaination on arrays please go to the PHP Manual and look under Arrays.
NOTE - Please note that with PHP the house address or key can be a integer or a string (Most programming languages differenciate between the two methods). So the string '100' or the integer 100 will work for this array in PHP.
In PHP to create an array you use the array() function. You can just create the variable empty or you can put something in it with or without a key. If the key is a string - in PHP it is called an associative array. And for the bold - you can put another array into the value making it multidimensional. I don't think I'll get into those today.
//empty array
$test1=array();
// this will make its own integer keys starting with zero and incrementing
// for each new value to be added
$test2 = array('Tom', 'Smith', '1 Main St.');
// load these values into these specific elements
$test3 = array('Fname'=>'Jane', 'Surname'=>'Brown', 'address'=>'5 State Ave') ;
// this one will take the max key value and add 1 to it to get the next key
$test4 = array(5=>'a', 10=>'b', 'c');
NOTE - Many languages require you to declare the maximum size of the array. That is not necessary with PHP.
To access the values in an array is as easy as making one. Simply tell it which element to give you.
echo $test2[1]; // this will give you 'Smith' - remember test2 started with zero echo $test3['address']; // This gives '5 State Ave'
Want only the last element of the array - use array_pop()... The first element of the array - array_shift()
What to cycle through the whole array and display the values? The foreach control structure will do it for you. It comes in 2 flavors for you programming palete. The first is nice and light and easy. It provides the value of the element and takes care of the iteration through the array all by itself.
foreach($test2 as $value)
{
echo $value;
}
But you want to access the array key also! You are so demanding. Very well. The second flavor of foreach allows you to access the array key as well as it's value and still takes care of the iteration through the array all by itself.
foreach($test3 as $key => $value)
{
echo $key .' = '. $value;
}
What about if you don't know what values you are going to put into an array? After all we are trying to make dynamic applications. There are a couple of ways you can add to an array. The simplest way is to just set a new value to the array. This can be done either with or without a you providing a key value.
$test3['[town'] = 'anywhere'; // with key $test4[15] = 'd'; // with key $test2[] = 'Howdy'; //without key
But what if you want to specifically add something to the front or end of an array ? Don't sweat it. You can use array_unshift() for adding to the front. Using it will move your new value to the beginning of the array and automatically adjust the numerical keys so the index starts at zero. To add to the end of the array use array_push() (unless your only adding 1 value then it is better to stick with the $array[] syntax).
Here is a list of very helpful functions associated with arrays. This is only a small handful of all the functions available to make your life easier when working with arrays. To see the full listing of functions available please go to the PHP Manual and look under Arrays Functions.
unset() - Need to remove one of the arrays key=> value pairs or get rid of the whole array? Remember this is different then setting the value to null.
unset($test4[11]); // this deletes the whole element from the array unset($test1); // this deletes the whole array
print_r() - Provides a quick and dirty way of seeing what is in array. I usually use it for debugging purposes (always want to make sure your data looks as you expect it to - especially when you creating it).
is_array() - Used to validate that the variable given actually is an array.
count() - Used to find out how many elements are in an array.
array_key_exists() - Used to see if a given key exists in an array.
array_keys() - Used to get all of the keys used in the array
explode() - Need to split a string up and load it into an array
implode() - or it's opposite - take an array and make it into a string
You have a form with a drop down list on it. For security reasons you should always make sure the data you receive is valid before using it (especially if it is going into your database)
<form method="post" action="process.php"> Favorite Nursery Rhyme: <select name="rhyme" > <option value="Old">Old McDonald</option> <option value="Mary">Mary Had a little lamb</option> <option value="Tea">I'm a little teacup</option> </select> <br /><br /> <input type="submit" name="submit"> </form>
This form is then sent to the process.php script in it we will make sure that the nursery rhyme submitted is one of the options (no Little Miss Muffit here - never did like that one). So how do we do this? By creating an array of valid answers and then making sure the data we recieved is one of them.
$myRhymes = array("Old", "Mary", "Tea");
if(in_array($_POST['rhyme'], $myRhymes)
{
echo 'You like one of my favorite nursery rhymes!';
}
else
{
echo 'That's not one of the choices for a nursery rhyme.';
}
Simple and very powerful - isn't it.
So there you have it. Hoped you enjoyed our walk down Array Street. And I hope you learned a couple of things along the way - mainly that arrays aren't something to be afraid of. Be daring - Go play around with them.

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 2.5 License
.