Part 1: What‘s an Array?
An array is a list of items, a bit like a shopping list. It allows you to store more than one item in only one variable.
Think of it like this. When writing your shopping list, you could use a separate piece of paper for each item you need to buy (a variable). However this is silly and unneeded—could you imagine how hard it would be to carry all that paper around with you? So, you use one piece of paper for all of your items. This one piece of paper is your array.
In the editor do you see the bit of text that starts with $array =
? That is our array. Don‘t worry about all the details just yet, we will explain in more detail later. For now, just see if you can work out what is happening.
<?php $array = array("Egg", "Tomato", "Beans"); ?>
Part 2: Array Syntax
Have you noticed something familiar at the start of our array? That‘s right, it starts in the same way as a variable, with the $
sign, and then a name, followed by =
.
However, this is when things start to get different. When declaring an array, we have to use array()
. This basically tells PHP that $array
is an array and not something else, such as a regular old variable.
By now, I am sure you have noticed the text inside the (
and )
. This is just the items in our array. So, currently, our array has the items "Egg," "Tomato," and "Beans" in it. You can add any type of information to an array, and you do it in much the same way as when declaring variables. Use""
when adding strings, and just enter the number when adding integers.
You must always remember, however, that each item in an array must be separated by a comma: ,
.
<?php // Add your array elements after // "Beans" and before the final ")" $array = array("Egg", "Tomato", "Beans" ); ?>
Part 3: Access by Offset with []
Each item in an array is numbered starting from 0. For example, when we create an array:
<?php $myArray = array("do", "re", "mi"); ?>
Each item is numbered starting from 0, like this:
+------+------+------+
| "do" | "re" | "mi" |
+------+------+------+
0 1 2
The item "do"
is in position 0, the item "re"
is in position 1, and so on.
Therefore, we can access a particular item of the array using its position, like this:
<?php $myArray = array("do", "re", "mi"); echo $myArray[0] // outputs "do" ?>
- First we create an array named
$myArray
- Then we use
echo
to output the first item in$myArray
. Since items are numbered starting from 0,"do"
is at position 0.
Part 4: Access by offset with {}
PHP is a very flexible language. When accessing arrays by offset, you can actually use two different types of syntax: the []
syntax we‘ve covered, or you can use curly braces ({}
). You use the curly braces just like you use the square brackets:
<?php $myArray = array("do", "re", "mi"); print $myArray{2}; // prints "mi"; ?>
Both forms are equivalent, and using one or the other is totally up to you!
Part 5: Modifying Array Elements
An item in an array can be changed by specifying its position and providing a new value, like this:
<?php $myArray = array("red", "blue", "yellow"); echo $myArray[1]; // outputs "blue" $myArray[1] = "green"; echo $myArray[1]; // outputs "green" ?>
- First we create a new array
$myArray
with a list of colors. - Then we output the item at position 1. Since items are numbered starting from 0,
"blue"
is at position 1 - Next we change the item at position 1 to
"green"
. - Now when we output the item at position 1, we get
"green"
.
Part 6: Deleting Array Elements
Finally, you can remove elements using unset
:
<?php $array = array("red", "blue", "green"); unset($array[2]); ?>
You can even delete the whole array:
<?php unset($array); ?>