PHP Tutorial - Part 3

Tutorial 4

Using Arrays

So far we have covered standard variables, lets look at arrays now. Arrays are variables that contain multiple units of data. Here is a typical array

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Here, the variable is split into four elements called 0, 1, 2 and 3. The values are 1, 2, 3 and 4.

1. Lets create that array now. Set up your file as usual and call it program5.php.

2. Create the variable array with the following value

PHP code:

$array = array(1, 2, 3, 4);

That's it!

Let's just explain that now. Firstly you are defining the variable is an array by saying array(). You are then adding in the values separated by commas which define a new array element.

3. To display all the data in an array, use the print_r() function. Below, write

PHP code:

print_r( $array );

The whole program should read

PHP code:

<?
$array = array(1, 2, 3, 4);
print_r( $array );
?>

Save, upload and view the file. It should print exactly what is written at the top

4. Now to echo just one of the values you use square brackets and number of the position in the array. So delete the second line (print_râ?¦) and type

PHP code:
echo $array[0];

The whole thing should look like

PHP code:

<?
$array = array(1, 2, 3, 4);
echo 'The first array value is '.$array[0];
?>

Save, upload and view the file. It should display

The first array value is 1

5. Arrays are handy for storing similar types of data (e.g. array 1 stores people's names and array 2 stores types of fruit). To assist in referencing the data, you can assign array keys.

Lets give it a go. Create a new file called program6.php, add the arrow question marks and enter in the following code.

PHP code:

$array=array(
'fruit1' => 'banana',
'fruit2' => 'apple',
'fruit3' => 'orange',
'fruit4' => 'grape'
);

So now your array has four elements called fruit1, fruit2, fruit3, fruit4 with the values banana, apple, orange, grape.

6. You can see this by using the following code

PHP code:

echo '<pre>';
print_r( $array );
echo '</pre>';

The whole code should look like this.

PHP code:

<?
$array=array(
'fruit1' => 'banana',
'fruit2' => 'apple',
'fruit3' => 'orange',
'fruit4' => 'grape'
);

echo '<pre>';
print_r( $array );
echo '</pre>';
?>

Don't worry about the <pre> echoes, this is just to display the array more clearly.

Save, upload and view the file. It should display

Array
(
[fruit1] => banana
[fruit2] => apple
[fruit3] => orange
[fruit4] => grape
)

Once you are happy with that, delete the lines

PHP code:

echo '<pre>';
print_r( $array );
echo '</pre>';

Now save this under the new filename of program7.php

You can now reference the array values by the key name, so under the array, enter the following code

PHP code:

echo 'I would love a '.$array[fruit1].' now';

Save, upload and view the file. It should display

I would love a banana now

That's it for arrays for now. We will return later on with array sorting and multi-dimensional arrays.

Oh just one thing, have another cookie.