How to Set up a PHP Array

 

In the code on the previous page, we had four items, and all with a different variable name: $Order_Number1, $Order_Number2, $Order_Number3, and $Order_Number4. With an array, you can just use a single name. You set up an array like this:

$Order_Number = array( );

First you type out what you want your array to be called ($Order_Number, in the array above) and, after an equals sign, you type this:

array( );

So setting up an array just involves typing the word array followed by a pair of round brackets. This is enough to tell PHP that you want to set up the array. But there's nothing in the array yet. All we're doing with our line of code is telling PHP to set up an array, and give it the name $Order_Number.

You can use two basic methods to put something into an array.


Method One – Type between the round brackets

The first method involves typing your values between the round brackets of array(). In the code below, we're setting up an array to hold the seasons of the year:

$seasons = array( "Autumn", "Winter", "Spring", "Summer" );

So the name of the array is $seasons. Between the round brackets of array(), we have typed some values. Each value is separated by a comma:

("Autumn", "Winter", "Spring", "Summer")

Arrays work by having a position, and some data for that position. In the above array, "Autumn" is in position zero, "Winter" is in position 1, "Spring" is in position 2, and "Summer" is in position 3.

The first position is always zero, unless you tell PHP otherwise. But the position is know as a Key. The Key then has a value attached to it. You can specify your own numbers for the Keys. If so, you do it like this:

$seasons = array( 1 => "Autumn", 2 => "Winter", 3 => "Spring", 4 => "Summer" );

So you type a number for your key, followed by the equals sign and a right angle bracket ( => ). In the array above, the first Key is now 1 and not 0. The item stored under key 1 is "Autumn". The last key is 4, and the item stored under key 4 is "Summer". Careful of all the commas, when you set up an array like this. Miss one out and you'll get error messages. Here's the keys and values that are set up in the array above:

1=> "Autumn",
2=> "Winter",
3=> "Spring",
4=> "Summer"

If you let PHP set the keys for you, it would be this:

0=> "Autumn",
1=> "Winter",
2=> "Spring",
3=> "Summer"

You can have numbers for the values of your keys. Here's an array that stores the numbers 10, 20, 30 and 40.

$Array_Name = array(10, 20, 30, 40);

Because no keys were specified, PHP will set your array up like this:

0=> 10,
1=> 20,
2=> 30,
3=> 40

Here's the same array again, only this time we're specifying our own key:

$Array_Name = array(1 => 10, 2 => 20, 3 => 30, 4 => 40);

This array will then look like this:

1=> 10,
2=> 20,
3=> 30,
4=> 40

So the key name is typed before the => symbol, and the data stored under this key is to the right.

You can store text and numbers in the same array:

$Array_Name = array(1 => 10, 2 => "Spring", 3 => 30, 4 => "Summer");

The above array would then look like this:

1=> 10,
2=> "Spring",
3=> 30,
4=> "Summer"


Method two – Assign values to an array

Another way to put values into an array is like this:

$seasons = array();

$seasons[ ]="Autumn";
$seasons[ ]="Winter";
$seasons[ ]="Spring";
$seasons[ ]="Summer";

Here, the array is first set up with $seasons = array();. This tells PHP that you want to create an array with the name of $seasons. To store values in the array you first type the name of the array, followed by a pair of square brackets:

$seasons[ ]

After the equals sign, you type out what you want to store in this position. Because no numbers were typed in between the square brackets, PHP will assign the number 0 as the first key:

0=> "Autumn",
1=> "Winter",
2=> "Spring",
3=> "Summer"

This is exactly the same as the array you saw earlier. If you want different numbers for your keys, then simply type them between the square brackets:

$seasons[1]="Autumn";
$seasons[2]="Winter";
$seasons[3]="Spring";
$seasons[4]="Summer";

PHP will then see your array like this:

1=> "Autumn",
2=> "Winter",
3=> "Spring",
4=> "Summer"

This method of creating arrays can be very useful for assigning values to an array within a loop. Here's some code:

$start = 1;
$times = 2;
$answer = array();

for ($start; $start < 11; $start++) {

$answer[$start] = $start * $times;

}

Don't worry if you don't fully understand the code above. The point is that the values in the array called $answer, and the array key numbers, are being assigned inside the loop. When you get some experience with arrays, you'll be creating them just like above!

 

In the next part, we'll take a look at how to get at the values stored in your arrays.