Arrays

I l @ ve RuBoard

An array is a great way to store data in an organized way that can be referenced for later use. In Figure 3.13, you can see that an array looks very much like a set of numbered cards; on each card is printed a value. Card 3 would have the value L printed on it.

Figure 3.13. An array structure.

graphics/03fig13.gif

Arrays have precise terms for these kinds of things; each card is called an array element. Each value in an array element is called an array value, and each array element is referenced by its array key. PHP lets you use several methods when working with arrays.

 <?php  $message[0] = "H";  $message[1] = "E";  $message[2] = "L";  $message[3] = "L";  $message[4] = "O";  while (list ($key, $val) = each ($message)) {      print("$val");       print("array key $key equals value $val");       print("<BR>");  }  ?> 

First, the array is created:

 $message[0] = "H";  $message[1] = "E";  $message[2] = "L";  $message[3] = "L";  $message[4] = "O"; 

The array variable type is used here. This is marked by the array element at the end of the variable. Note that the array starts at 0; in PHP, all arrays start at 0. In this example, $message[1] equals E. Of course, now that you have an array, you must be able to work with it. PHP lets you run through the contents of an array as follows :

 while (list ($key, $val) = each ($message)) {       print("$val");        print("<BR>");  } 

A while loop loops through the array. The array is broken into its element key and element value using the list statement and the each statement to work through each element in the array in turn . Looping through the array, each element value is displayed:

 print("array key $key equals value $val"); 

If you run the script, you can see that the element values are displayed.

When setting element keys, you don't have to use numbers. You can use any value you like. You can change the preceding script to use letters instead of numbers as follows:

 <?php  $message["A"] = "H";  $message["B"] = "E";  $message["C"] = "L";  $message["D"] = "L";  $message["E"] = "O";  while (list ($key, $val) = each ($message)) {      Print("$val");       Print("<BR>");  }  ?> 

You can also look up values in an array using the element key:

 <?php  $message["A"] = "H";  $message["B"] = "E";  $message["C"] = "L";  $message["D"] = "L";  $message["E"] = "O";  while (list ($key, $val) = each ($message)) {      if($key == B) {           Print("The array key is $key and its value is $val");       }  }  ?> 

This code looks up an element key called B and displays its value, as shown in Figure 3.14.

Figure 3.14. An array element value found through its element key.

graphics/03fig14.gif

PHP gives you another syntax for creating arrays using the array statement:

 <?php  $message = array ("A"=>"H", "B"=>"E", "C"=>"L", "D"=>"L", "E"=>"O");  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  ?> 

The variable $message contains the array that is created using the array statement. The array statement lets you create arrays by defining first the element key and then the related element value. If you run the script, you can see the structure the array statement has created, as shown in Figure 3.15.

Figure 3.15. Array element keys and related values created with the array statement.

graphics/03fig15.gif

The array statement can also add element key values for you:

 <?php  $message = array ("H", "E", "L", "L", "O");  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  ?> 

Here, only array element values within the array statement are defined. If you run the script, you can see that the array statement adds the element keys for you, as shown in Figure 3.16.

Figure 3.16. Array element keys created automatically by the array statement.

graphics/03fig16.gif

Sorting Arrays

Sometimes arrays can be messy; the element keys and values can be in a mixed order, and sometimes it's necessary to put them in order. Writing code to sort them can be time-consuming (even when you're adapting scripts from others). PHP thankfully provides a collection of statements to let you do this.

The following sections discuss four common sort statements for sorting an array by its element values or by its element keys. However, note that PHP also has other sort statements that you can use.

The sort Function

The sort statement lets you sort array values from the lowest to the highest value:

 <?php  //sort array from lowest value to highest  $numbers = array (5, 2, 3, 1, 4);  //unsorted  print("unsorted array<BR>");  while (list ($key, $val) = each ($numbers)) {      print("array key $key has value $val");       print("<BR>");  }  Print("<BR>");  //sorted  print("sorted array<BR>");  sort($message);  while (list ($key, $val) = each ($message)) {      print("array key $key has value $val");       print("<BR>");  }  ?> 

This code creates an array and allows the array statement to create the element keys for you. The script then displays the unsorted array, sorts the array using the sort statement, and displays the sorted array.

The asort Function

When using the sort statement, note how the array keys change. Before the sort element key, 0 held the element value of 5, and after the sort, it contained the element value of 1. You can keep element key and value pairs related but at the same time sort the element values from highest to lowest. How? You use the asort function:

 <?php  //sort array from lowest value to highest but maintain key  $message = array ("A"=>"1", "B"=>"5", "C"=>"2", "D"=>"3", "E"=>"4");  //unsorted  print("unsorted array<BR>");  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  Print("<BR>");  //sorted  print("sorted array<BR>");  asort($message);  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  ?> 

This code creates an array. Note that this example specifies element keys and values but, like the sort statement, you can allow the array statement to create element keys for you. The script then displays the unsorted array, sorts the array using the asort function, and displays the sorted array, as shown in Figure 3.17.

Figure 3.17. An array sorted from lowest element value to highest but with relative element keys using the asort statement.

graphics/03fig17.gif

The array element values have been sorted, but they relate to the same element keys before and after the sort. For example, element key E contains the value 4 before and after the sort.

The rsort Function

You can also sort array element values from the highest value to the lowest value using the rsort function:

 <?php  //sort array from highest value to lowest  $message = array (5, 2, 3, 1, 4);  //unsorted  print("unsorted array<BR>");  while (list ($key, $val) = each ($message)) {      print("array key $key has value $val");       print("<BR>");  }  Print("<BR>");  //sorted  print("sorted array<BR>");  rsort($message);  while (list ($key, $val) = each ($message)) {      print("array key $key has value $val");  print("<BR>");  }  ?> 

This script is the same as the sort script, except that it uses the rsort function to sort the array from highest to lowest.

The ksort Function

PHP lets you sort an array by its element keys from the lowest value to the highest using the ksort function:

 <?php  //sort array from lowest key to highest  $message = array ("B"=>"1", "C"=>"5", "D"=>"2", "A"=>"3", "E"=>"4");  //unsorted  print("unsorted array<BR>");  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  Print("<BR>");  //sorted  ksort($message);  print("sorted array<BR>");  while (list ($key, $val) = each ($message)) {      Print("The array key is $key and its value is $val");       Print("<BR>");  }  ?> 

This script is the same as the previous examples, but it uses the ksort statement to sort the array.

In the script, note that I have kept the original element keys of letters. When sorted using the ksort statement, the keys are placed in alphabetical order. PHP sorts alphabetical element keys or values in the same way.

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

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