5.3. Storing Data in ArraysStoring a value in an array will create the array if it didn't already exist, but trying to retrieve a value from an array that hasn't been defined yet won't create the array. For example: // $addresses not defined before this point echo $addresses[0]; // prints nothing echo $addresses; // prints nothing $addresses[0] = 'spam@cyberpromo.net'; echo $addresses; // prints "Array" Using simple assignment to initialize an array in your program leads to code like this: $addresses[0] = 'spam@cyberpromo.net'; $addresses[1] = 'abuse@example.com'; $addresses[2] = 'root@example.com'; // ... That's an indexed array, with integer indexes beginning at 0. Here's an associative array: $price['Gasket'] = 15.29; $price['Wheel'] = 75.25; $price['Tire'] = 50.00; // ... An easier way to initialize an array is to use the array( ) construct, which builds an array from its arguments: $addresses = array('spam@cyberpromo.net', 'abuse@example.com', 'root@example.com'); To create an associative array with array( ), use the => symbol to separate indexes from values: $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); To construct an empty array, pass no arguments to array( ): $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 ArrayTo insert more values into the end of an existing indexed array, use the [] syntax: $family = array('Fred', 'Wilma'); $family[] = 'Pebbles'; // $family[2] 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[0] is now 'Wilma' 5.3.2. Assigning a Range of ValuesThe range( ) function creates an array of consecutive integer or character values between the two values you pass to it as arguments. For example: $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 ArrayThe 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 ArrayTo 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); If you pad an associative array, existing keys will be preserved. New elements will have numeric keys starting at 0. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|