[ Team LiB ] Previous Section Next Section

Creating Arrays

By default, arrays are lists of values indexed by number. Values can be assigned to an array in two ways: with the array() construct or directly using empty square brackets ([]). You'll meet both of these in the next two sections.

Defining Arrays with the array() Construct

The array() construct is useful when you want to assign multiple values to an array at one time. Let's define an array called $users and assign four strings to it:

$users = array ("Bert", "Sharon", "Betty", "Harry");

You can now access the third element in the $user array by using the index 2:

print $users[2];

This would return the string Betty. The index of an array element is placed between square brackets directly after the array name. You can use this notation to either set or retrieve a value.

Remember that arrays are indexed from zero by default, so the index of any element in a sequentially indexed array always is one less than the element's place in the list.

Defining or Adding to Arrays with the Array Identifier

You can create a new array (or add to an existing one) by using the array identifier in conjunction with the array name. The array identifier is a set of square brackets with no index number or name inside it.

Let's re-create our $users array in this way:

$users[] = " Bert";
$users[] = " Sharon";
$users[] = " Betty";
$users[] = " Harry";

Notice that we didn't need to place any numbers between the square brackets. PHP automatically takes care of the index number, which saves you from having to work out which is the next available slot.

We could have added numbers if we wanted, and the result would have been exactly the same. It's not advisable to do this, though. Take a look at the following code:

$users[0] = " Bert";
$users[200] = "Sharon";

The array has only two elements, but the index of the final element is 200. PHP will not initialize the intervening elements, which could lead to confusion when attempting to access elements in the array. On the other hand, in some circumstances you will want to use arbitrary index numbers in your array.

In addition to creating arrays, you can use the array identifier to add new values onto the end of an existing array. In the following code, we define an array with the array() construct and use the array identifier to add a new element:

$users = array ("Bert", "Sharon", "Betty", "Harry");
$users[] = "Sally";

Populating an Array with array_fill()

If you want to pad an array with default values, you can use the array() function, like so:

$membertypes = array ("regular", "regular", "regular", $regular");

You could also use the empty brackets approach:

$membertypes[] = "regular";
$membertypes[] = "regular";
$membertypes[] = "regular";
$membertypes[] = "regular";

PHP provides a flexible function to automate this task. array_fill() requires three arguments: a number representing the index from which to start filling, another integer representing the number of elements to populate, and the value to add to the array. Using array_fill(), we can rewrite the previous fragments:

$membertypes = array_fill( 0, 4, "regular" );

    [ Team LiB ] Previous Section Next Section