Adding Items to an Array

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.

graphics/07sc02.jpg

To add elements to an array:

  1. Open soups.php in your text editor, if it is not already.

  2. After the array is initialized , using array(), add (Script 7.2):

     $HowMany = count($Soups); print ("The array contains $HowMany elements.<P>\n"); 

    The count() function will determine how many elements are in $Soups. By assigning that value to a variable, you'll be able to easily print it out.

  3. Add three more elements to the array.

     $Soups["Thursday"] = "Chicken Noodle"; $Soups["Friday"] = "Tomato"; $Soups["Saturday"] = "Cream of Broccoli"; 
  4. Recount how many elements are in the array and print this value.

     $HowManyNow = count($Soups); print ("The array now contains $HowManyNow elements.<P>\n"); 
  5. Save your script, upload it to your server, and test in your Web browser (Figure 7.4).

    Figure 7.4. A direct way to insure that the new elements were successfully added to the array is to count the number of elements before and after the additions were made.

    graphics/07fig04.gif

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.

graphics/07sc03.jpg

To merge two arrays:

  1. Open soups.php in your text editor, if it is not already.

  2. After the $Soups array is initialized, count and print the number of elements it contains (Script 7.3):

     $HowMany = count($Soups); print ("The $Soups array contains $HowMany elements.<P>\n"); 
  3. Create a second array, then count and print the number of elements it contains.

     $Soups2 = array(  "Thursday"=>"Chicken Noodle",  "Friday"=>"Tomato",  "Saturday"=>"Cream of Broccoli" ); $HowMany2 = count($Soups2); print ("The $Soups2 array contains $HowMany2 elements.<P>\n"); 
  4. Merge the two arrays into one new array.

     $TheSoups = array_merge ($Soups,  $Soups2); 

    Make sure you put the arrays in this order ($Soups then $Soups2) so that the Thursday through Saturday elements will be appended to the Monday through Wednesday elements and not the other way around.

  5. Count and print the number of elements in the newly created array.

     $HowMany3 = count($TheSoups); print ("The $TheSoups array contains $HowMany3 elements.<P>\n"); 
  6. Close the PHP and the HTML document.

  7. Save the file, upload it to your server, and test in your Web browser (Figure 7.5).

    Figure 7.5. The count() function is used here to insure that all of the elements of both arrays are present in the newly created array.

    graphics/07fig05.gif

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


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net