What is an array?
An array is a list of a certain variable type, where each item in the list can be referenced by a unique index number, usually starting at 0. Think about it in real terms as you might think about a shopping list. EG:
Now imagine that you had one variable to hold them all:
And each item in the list, had an identifier linked with the one variable:
  0 = Cheese
    1 = Eggs
    2 = Milk
    3 = Bread
In PHP this would look like this:
$Shopping-list[0] = "Cheese";
$Shopping-list[1] = "Eggs";
$Shopping-list[2] = "Milk";
$Shopping-list[3] = "Bread";
As you can see from above what we actually have is a list of strings, and we can do exactly the same with numbers:
$numbers[0] = 21;
$numbers[1] = 50;
$numbers[2] = 43;
Like any other variable type, PHP will automatically define a variable type and set it up for you when you start using it, however in most cases (and it's good practice) you should usually pre-declare your intention to use an array, especially if you don't know in advance what your going to be storing in it. You can do this by using the following PHP function.
$myArray = array();
This will declare an empty array called "myArray" ready to start accepting additions.
To add items to it, you can use the null index format, which will then add the new item at the next available free index:
	$myArray[] = "Hello";
	$myArray[] = "World";
This will allocate "hello" to slot 0, and "world" to slot 1. You can delete an element at an index by using the unset function:
We'll leave only $myArray[0] containing "hello". A word of warning though, using unset DOES NOT reorder the indexes, I've lost count of how many times this has bitten me in a program, only to examine an array's contents and find holes in the indexes, so always be sure your code is ready and able to detect a gap in an array if your going to do this.
A better way to remove elements is to use the "array_splice" function, simply by giving it the index to remove and a length of 1 as follows:
We'll remove element number 1 and then reorder the indexes, try the following to see what I mean:
	$myArray = array("one","two","three","four");
You'll see that "two" gets removed and the indexes reordered to close the hole, also if you want to keep a copy of the removed elements in another array, you can assign a variable to the result of the function, and the deleted elements will be stored in it, eg:
	$deleted = array_splice($myArray,1,1);
What's the deal with starting at 0?
Honestly? I really don't know, I'm guessing it's because of PHP's similarity to the C programming language, and traditionally all versions of C and C++ start array indexes at 0, it may also be to do with the way loops work, all I know is that if the PHP function "count()" returns a number other than 0, then your highest index is that number minus 1.
Arrays don't always have to hold the same types
Just because an array is set up as a 'shopping list' or 'age list' it does not mean that you have to always put that type of data in it, you can freely mix and match data types and PHP will adapt automatically, eg:
$myArray = array();
$myArray[] = "Peter";
$myArray[] = "Shaw";
$myArray[] = 21;
As you can see I've added 2 text strings and 1 number (Which I wish was my age again : )
This shows that you could, in actual fact, store a complete record of mixed information in memory, the system is so flexible that you can even store an array, eg:
$myArray = array();
$myArray[] = "Peter";
$myArray[] = "Shaw";
$myArray[] = 21;
$myArray[] = array("Computers","Programming","Electronics");
If you use "print_r" to display this, you'll get the following:
    [0] => "Peter"
    [1] => "Shaw"
    [2] => 21
    [3] => Array
            	[0] => "Computers"
            [1] => "Programming"
            [2] => "Electronics"

print_r is a fantastic function for debugging, because it displays the array structure and it's contents in a nice neat tree diagram, if you are using print_r in a web page though, you will want to print an opening and closing pre tags (<pre> </pre>) on either side of it to preserve the layout.
Nested arrays are great for representing complex data, unfortunately you still need to remember your indexes and the deeper you go the more complex this becomes, how do you remember that "$myArray[0][3][2][1]" is equal to the first name of the second cousin in the third family of the family tree? That's solved by the one thing we've not covered yet and that's hashes.
Array elements have names
A hash is pretty much a mixed format array, the major difference is that names are used rather than index numbers. Taking the example above we could now write the following:
$myHash = array();
$myHash['firstname'] = "Peter";
$myHash['lastname'] = "Shaw";
$myHash['age'] = 21;
$myHash['hobbies'] = array(0 => "Computers", 1 => "Programming", 2 => "Electronics");

We then print_r it:
  [firstname] => Peter
  [lastname] => Shaw
  [age] => 21
  [hobbies] => Array
    [0] => Computers
    [1] => Programming
    [2] => Electronics

You can see straight away how much more descriptive it is, so I can now do things like:
Print "First name is : " . $myArray['firstname'];
Which will result in this being displayed:
"First name is : Peter"
I can also show a hobby by using it:
Print "First hobby is : " . $myArray['hobbies'][0];
You should also see that in the second array we now create the indexes using the arrow syntax:
0 => "Computers";
This is telling PHP to use 0 as the index, we could just as easily used:
"Hobby0" => "Computers"
By using the arrow syntax we can define not only the contents of a hash at creation time, but the names the elements use in a nice neat easy to read way.
I'll show you how to loop over the elements next time when we cover loops and decisions, but for now we'll leave it there.
In Summary
Today we learned how to represent our data in memory using complex collections to keep our data organized. Once you start using databases you'll quickly realize that using these techniques to model your tables in memory will greatly assist you with display and management of your data in web pages, hashes are also a large part of object orientated programming and allow you to keep a tight hold of your class related data when using that programming model. I'm not going to cover class programming in this series however because that is quite a complex subject.
As always, I encourage you to look at the PHP Manual, and explore the functions available for working with arrays and hashes, the section to read can be found at http://www.php.net/manual/en/book.array.php so until next time.
Happy hashing.
The ABC's of PHP
Introduction to PHP
What do I need to make it work?
Basic Script Building in PHP
How Variable Am I?
Strings & Text
Math & Number Handling in PHP
Introduction to Arrays and Hashes in PHP
Loops and Decisions in PHP
Advanced String Processing - How Regular Are Your Expressions