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:
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 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:
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:
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" 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:
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'];
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.
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.