Welcome back to my little series of PHP novice tutorials. As previously mentioned, I will move on to more advanced tutorials in a month or so, but I might randomly throw in one or two of those while I’m at it. I dunno.

Let’s move on to arrays.

What the hell is an array? Well, an array is a collection of values or more arrays.

Example 1: an array of fruits

<?php

$fruits = array('apple''banana''cherry''durian');

?>

You can’t directly echo an array.

Example 2: echoing the array

<?php

// By the way, I used double quotes because of \n
// which represents a newline. Other common special
// characters include \s for space, \t for tab,
// \r for carriage return and \\ for a literal backslash.
echo "with echo()\n";
// You can't do this.
echo $fruits;
echo "\n\n";
// But you can try print_r().
echo "with print_r()\n";
print_r($fruits);

?>
with echo()
Array

with print_r()
Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

You can add to it like this:

Example 3: adding more values to the array

<?php

$fruits[] = 'durian';
$fruits[] = 'eggplant';
print_r($fruits);

?>
Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => durian
    [4] => eggplant
)

Notice how the array starts with 0? All arrays in most programming languages start with the index 0, not 1.

You can give the fruits names instead of leaving them with numbers.

The ‘names’ I mentioned are actually called keys, and the values… are values.

You can give a custom key to a value like this:

Example 4: a key and a value

<?php

$fruits = array('red' => 'apple''yellow' => 'banana');
print_r($fruits);

?>
Array
(
    [red] => apple
    [yellow] => banana
)

We use the => operator to assign a value to a key while defining an array.

Between the square braces after an array’s name is a key. That means you can overwrite a key or assign a new one, like so:

Example 5: assigning a new key

<?php

$names = array(
    // first:last
    'john' => 'doe', 
    'james' => 'atkinson'
);
// We DON'T use the key-value operator here!
// Use the assign (=) operator.
$names['matt'] = 'mullenweg';
print_r($names);

?>
Array
(
    [john] => doe,
    [james] => atkinson,
    [matt] => mullenweg
)

How about storing arrays in arrays? PHP can do that too! These big boys are called multidimensional arrays.

Example 6: a multidimensional array

<?php

$array = array(=> array('key' => 'value'));
print_r($array);

?>
Array
(
    [0] => Array
        (
            [key] => value
        )
)

Simple. To reference a multidimensional array, you write like so:

Example 7: referencing a multidimensional array

<?php

$array = array(=> array('key' => 'value'));

// Referencing an array in that array
$array[0]['key2'] = 'value2';

// You can even mix arrays and scalars (things that aren't arrays)
// in the same array!
$array[] = 'scalar';

print_r($array);

?>
Array
(
    [0] => Array
        (
            [key] => value
            [key2] => value2
        )
    [1] => scalar
)

Remember that these are just the basics. We’ll continue on to more sophisticated topics some other day - it’s pretty much about time we get started on some functions!

Back to top