5.3. Storing Data in Arrays
// $addresses not defined before this point echo $addresses; // prints nothing echo $addresses; // prints nothing $addresses = 'email@example.com'; echo $addresses; // prints "Array"
$addresses = 'firstname.lastname@example.org'; $addresses = 'email@example.com'; $addresses = 'firstname.lastname@example.org'; // ...
$price['Gasket'] = 15.29; $price['Wheel'] = 75.25; $price['Tire'] = 50.00; // ...
$addresses = array('email@example.com', 'firstname.lastname@example.org', 'email@example.com');
$price = array('Gasket' => 15.29, 'Wheel' => 75.25, 'Tire' => 50.00);
Notice the use of whitespace and alignment. We could have bunched up the code, but it wouldn't have been as easy to read:
$price = array('Gasket'=>15.29,'Wheel'=>75.25,'Tire'=>50.00);
$addresses = array( );
You can specify an initial key with => and then a list of values. The values are inserted into the array starting with that key, with subsequent values having sequential keys:
$days = array(1 => 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'); // 2 is Tuesday, 3 is Wednesday, etc.
If the initial index is a non-numeric string, subsequent indexes are integers beginning at 0. Thus, the following code is probably a mistake:
$whoops = array('Friday' => 'Black', 'Brown', 'Green'); // same as $whoops = array('Friday' => 'Black', 0 => 'Brown', 1 => 'Green');
5.3.1. Adding Values to the End of an Array
$family = array('Fred', 'Wilma'); $family = 'Pebbles'; // $family is 'Pebbles'
This construct assumes the array's indexes are numbers and assigns elements into the next available numeric index, starting from 0. Attempting to append to an associative array is almost always a programmer mistake, but PHP will give the new elements numeric indexes without issuing a warning:
$person = array('name' => 'Fred'); $person = 'Wilma'; // $person is now 'Wilma'
5.3.2. Assigning a Range of Values
$numbers = range(2, 5); // $numbers = array(2, 3, 4, 5); $letters = range('a', 'z'); // $numbers holds the alphabet $reversed_numbers = range(5, 2); // $numbers = array(5, 4, 3, 2);
Only the first letter of a string argument is used to build the range:
range('aaa', 'zzz') /// same as range('a','z')
5.3.3. Getting the Size of an Array
The count( ) and sizeof( ) functions are identical in use and effect. They return the number of elements in the array. There is no stylistic preference about which function you use. Here's an example:
$family = array('Fred', 'Wilma', 'Pebbles'); $size = count($family); // $size is 3
These functions do not consult any numeric indexes that might be present:
$confusion = array( 10 => 'ten', 11 => 'eleven', 12 => 'twelve'); $size = count($confusion); // $size is 3
5.3.4. Padding an Array
To create an array initialized to the same value, use array_pad( ). The first argument to array_pad( ) is the array, the second argument is the minimum number of elements you want the array to have, and the third argument is the value to give any elements that are created. The array_pad( ) function returns a new padded array, leaving its argument array alone.
Here's array_pad( ) in action:
$scores = array(5, 10); $padded = array_pad($scores, 5, 0); // $padded is now array(5, 10, 0, 0, 0)
Notice how the new values are appended to the end of the array. If you want the new values added to the start of the array, use a negative second argument:
$padded = array_pad($scores, -5, 0);
Assign the results of array_pad( ) back to the original array to get the effect of an in situ change:
$scores = array_pad($scores, 5, 0);
Copyright © 2003 O'Reilly & Associates. All rights reserved.