I l @ ve RuBoard |
In PHP, once an array exists, you can add extra elements to your array by directly assigning them with the assignment operator (the equal sign), similar to how you assign a value to a string or a number. When doing so, you can either specify the key of the added element or not, but in either case, you must refer to the array with the square brackets. To add two items to the $List list, you would write: $List[] = "pears"; $List[] = "tomatoes"; If you do not specify the key, each element will be appended to the existing array, indexed with the next logical number. Assuming that this is the same array from the previous section, where it was indexed at 1, 2, and 3, pears is now located at 4 and tomatoes at 5. If you do specify the index, the value will be assigned at that location and any existing value already indexed at that point will be overwritten, like so: $List[3] = "pears"; $List[4] = "tomatoes"; Now, the value of the element in the fourth position of the array is tomatoes and no element of $List is equal to oranges. With this in mind, I would caution that unless you intend to overwrite any existing data, you'll be better off not including a specific key when adding values to your arrays. (However, if the array uses strings for indices, you'll want to specify keys so that values do not get lost.) To test this process, you'll rewrite soups.php to add more elements to the array. In order to see the difference adding more elements makes, you'll print out how many elements were in the array before and after the new additions. Just as you can find the length of a stringhow many characters it containsusing strlen(), you can also determine the number of elements in an array, using count(). $HowMany = count($Array); Script 7.2. You can directly add elements to an array one at a time by assigning each element a value with the assignment operator. The count() function will help you keep track of how many elements the array contains.
To add elements to an array:
New to PHP 4.0 is a function that allows you to append one array onto another. Think of it as concatenation for arrays. The function, array_merge(), works like so: $NewArray = array_merge ($OneArray, $TwoArray); You can rewrite the soups.php page using this new function if you are working with a server that has PHP 4.0. Script 7.3. The array_merge() function is new to PHP 4. It's just one of several new array functions designed to save the programmer time.
To merge two arrays:
Tip Be very careful when directly adding elements to an array. There's a correct way to do it ( $Array[] = "Add This"; or $Array[1] = "Add This"; ) and an incorrect way ( $Array = "Add This"; ). Forgetting to use the brackets will result in the added value replacing the entire existing array and you will be left with a simple string or number. Tip PHP 4 includes a slew of new functions for handling arrays not all of which are covered here. The PHP manual, available through the PHP home page, discusses them all. Be careful not to use a PHP 4 specific function if your server is running PHP 3. |
I l @ ve RuBoard |