Arrays are fundamental! In this post we will first be looking at what exactly an array is in the context of php, and how we build arrays. We will then go on to look at the many different ways we can access information in an array and how we can manipulate the data in one. Without further ado…
What is a Variable?
If you have been following the tutorials on this site, you will be familiar with the concept of a variable in php – how we can assign a value to a variable and then access this value later on in the code, or manipualte the value. To put it in simple terms, it’s like putting something into a suitcase and giving that suitcase a certain name. From then on, we will always be able to access what is in that suitcase, simply by using the name we assigned it. In this way we can store quite complex or lengthy information in a simple fashion. To give an example, let’s say we have a encyption key that we need to access throughout the code:
Ok, that’s complicated. We don’t want to be remembering that and typing it out everytime we need to access it in the code. Instead, we put in a varaible, named appropriately so we know exactly what is in the variable at first glance:
$key = df8$RrsdSDsdfSFKSFDsd4w34r345345;
Magic. Now in the code all we need to do is reference ‘$key’ whenever we want to use the value.
What is an array?
So that’s a variable, but what about arrays? That is after all what this post is about. The simplest way to think of an array is as a collection of associated variables. The easiest way to demonstrate is with an example.
Lets say we are building a shopping basket application, and we want a simple way to store information about the food being bought. The first thing we could do is create a number of variables for what we have bought:
$item1 = "banana"; $item2 = "cheese"; $item3 = "apple"; $item4 = "orange"; $item5 = "milk";
Ok, we have stored the information, but not in a very clear way. There is no indication as to which food item is in which variable, and the names of the variables aren’t particularly helpful. A better way to store this information would be with an array. This is how we would do this in php:
First we declare and populate the array:
$shopping_list = array( 'banana', 'cheese', 'apple', 'orange', 'milk' );
So now we have the full shopping list stored conveniently in one array, called ‘$shopping_list’, which makes semantic sense.
Accessing an Array
Once we have all the information in the array, we need to have a way of accessing it. We can do this easily in php by referenceing the index of an array. To explain – with a simple array such as our shopping list, you can think of each item in the list as having an associated number, or index. The index starts at 0, and increments for each item in the array. So for our $shopping_list, the following indexes will be assigned:
array( 'banana', 'cheese', 'apple', 'orange', 'milk' ) index: 0 1 2 3 4
Note that the indexing of arrays starts with ‘0’. You can easily see this by printing out the array:
print_r( $shopping_list );
will give us:
Array (  => banana  => cheese  => apple  => orange  => milk )
So, to access the first item in the array, ‘banana’, we simply reference the first index:
To access the third item:
We put the index we wish to access in the square brackets that follow the array name.
Manipulating an Array
It is also quite easy to change, delete or add items in the array.
Say we no longer want a banana, but we want a mango instead. In this case, given we know what position the banana is at – 0 – we can directly alter this using the folllwoing code:
$shopping_list = 'mango';
If we print out the array we will now see our new list:
print_r( $shopping_list );
will give us:
Array (  => mango  => cheese  => apple  => orange  => milk )
Removing at item is also easy. Let’s say we no longer want any milk. We use ‘unset':
unset( $shopping_list );
Now printing out the array will give us the following:
Array (  => mango  => cheese  => apple  => orange )
And finally, adding an element to our array is also easy. Let’s say we want some ice cream – there are two ways we can do this. The first is using ‘array_push':
array_push( $shopping_list, 'ice cream' );
This will add ‘ice cream’ to the end of our array, as will the following:
$shopping_list = 'ice cream';
Either one giving:
Array (  => mango  => cheese  => apple  => orange  => ice cream )
There are many, many more ways to manipulate an array than the few examples I have given above. If there is something you want to do with the data in an array, there’s more than likely a way to do it. Again, Google is always your best friend!
NB – in the above example you will see that the indexing has not been preserved. This is because we ‘unset’ the value of ‘milk’ – we removed it from the array completely, index and all. To have removed milk, but kept the index, we would do the following:
$shopping_list = '';
Printing this out will deomstrate that the value has been removed, but the index is still available:
Array (  => mango  => cheese  => apple  => orange  =>  => ice cream )
We can see above how useful arrays are in organising our data. We see how each item is given an index that we can use to access its value. This may be all we need, but there will be some instances where it will be useful to have more informative indexes in our arrays. A good example of this would be in a registration form. Once we get the information from the form, it would make sense to store the name, address, etc in an array, but will having the values indexed by number be useful? Not really. We would have to remember the position in the array of the first name, the surname, the postcode etc to be able to access them. Instead it would make more sense to use an associative array. As always, the best way to explain is to demonstrate.
Say we want to store the first name and surname in an array that we will call $person. First we declare the array:
$person = array();
In an associative array, the index is known as the key, and we assign a value to each key. For our person array we would do the following:
$person[ 'first_name' ] = 'Jimi'; $person[ 'surname' ] = 'Hendrix';
Now if we print out the array, we will see the following:
Array ( [first_name] => Jimi [surname] => Hendrix )
We can access and manipulate this array in the same way as we do with a normal array, using keys instead of indexes.
echo $person[ 'first_name' ];
Try the following, printing out the array afterwards to see the changes:
$person[ 'first_name' ] = 'bob'; $person[ 'middle_name' ] = 'phil'; $person[ 'middle_name' ] = ''; unset( $person[ 'middle_name' ] );
So there you have it, everything you will ever need to know about arrays. Haha, that’s just a little joke. You’ll be learning about arrays for a long time – they are a very powerful and fundamental part of programming and there are hundreds of ways to manipulate and manage arrays. The above is a great start though, and if you get the basics down, you can’t go far wrong.
We have recapped variables, learnt what arrays and associative arrays are, and how to manipulate the data in these arrays. In the next tutorial I will cover loops, for which a solid knowledge of arrays will be useful, so make sure you understand what’s been covered in this tutorial! As always, put any questions you have in the comments. See you in the next tutorial.
If you would like to be kept up to date with new posts here at the web developing cat, then be sure to add your name to our mailing list below.