Accessing Array Elements

I l @ ve RuBoard

Regardless of how you establish your array, there is only one way to retrieve a specific element (or value) from it, and that is to refer to its index. One option for handling arrays is to assign a specific element's value to a separate variable using the assignment operator:

 $Total = $Array[1]; 

By doing this you can preserve the original value within the array and still manipulate it separately as a variable.

However, you can also refer to a specific element's value directly and use it as you would a string in most circumstances:

 print ("The total of your order comes  to $Array[Total]"); 

When printing from an array, you must omit the double quotation marks that you would normally use around the index as they conflict with the print() statement itself. This line will cause you problems:

 print ("The total of your order comes  to $Array["Total"]"); 

Here's an example where using quotation marks will not be problematic :

 $Array["Name"] = trim($Array["Name"]); 

Ironically, the feature which makes arrays so usefulbeing able to store multiple values within one variablealso gives it a limitation that the other variable types do not have, namely, that you must know the keys of the array in order to access its elements. If the array was set using strings, such as the $Soups array, referring to $Soups[1] will point to nothing. For that matter, since variables are case-sensitive, $Soups["monday"] will be worthless as you indexed Clam Chowder at $Soups["Monday"].

Fortunately, the fastest and easiest way to access all the values of an array is to use a loop, in conjunction with the each() function. This function creates an array of the index and values of the array. Within a for loop, each() can be repeated as many times as there are array elements until every value has been returned.

 for ($n = 0; $n < count($Array); $n++) {    $Line = each ($Array);    print ("Key is equal to $Line[key].<BR>Value is equal to  $Line[value]."); } 

In this example the each() function will create an array called $Line that contains the key and value for the $Array at its current position. You can think of the array as having an internal pointer. Upon first using the each() function, the pointer is at $Array's first element. The each() function will retrieve those values and assign them to $Line[key] (as well as $Line[0]) and $Line[value] (also $Line[1]), then move the pointer ahead one element. The second time each() is called it will retrieve the second set of values and the process will be repeated until there are no more elements in the array and the loop is exited.

You can now rewrite the soups.php script to use this knowledge. Instead of merely being able to print out how many elements are in an array (as you've done to this point), you will now be able to access the actual values.

To print the values of any array:

  1. Create a new PHP document in your text editor.

  2. Write the standard HTML header (Script 7.4).

     <HTML><HEAD><TITLE>Using Arrays </TITLE><BODY> 
    Script 7.4. A loop is the most common way to access every element in an array. In this script, the each() function is used to determine the array's keys and values, which are then printed to the Web browser.

    graphics/07sc04.jpg

  3. Start the PHP section of the page and initialize the $Soups array.

     <?php $Soups = array( "Monday"=>"Clam Chowder", "Tuesday"=>"White Chicken Chili", "Wednesday"=>"Vegetarian", "Thursday"=>"Chicken Noodle", "Friday"=>"Tomato", "Saturday"=>"Cream of Broccoli" ); 
  4. Begin a for loop to access every element in the array.

     for ($n = 0; $n < count($Soups); $n++) { 

    This for loop assigns 0 to the dummy variable $n. It will then check to see if $n is less than the number of items in the array. If so it will execute the loop then increment the $n variable.

  5. Use the each() function to retrieve the keys and values, then print them out.

     $Line = each ($Soups);         print ("$Line[key]'s soup is     $Line[value].<P>\n"); 

    The loop will assign the keys and values of the array to the $Line array via the each() function. It will then print out the key and the value.

  6. Close the loop, the PHP and the HTML page.

  7. } ?></BODY></HTML>

  8. Save the page as soups.php, upload to your server, and test in your Web browser (Figure 7.6).

    Figure 7.6. The execution of the loop for every element in the array generates this page. The each() function allows the PHP to access each key and value without prior knowledge of what they were.

    graphics/07fig06.gif

Tip

An alternative is to set a variable to the value of count($Array) and use that in your loop. This way, the PHP does not have to recount the array on each iteration.


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