11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 5 • ARRAYSConsider an example. Suppose you want to sort exam grades from lowest to highest:$grades = array(42, 98, 100, 100, 43, 12);sort($grades);print_r($grades);The outcome looks like this:Array ( [0] => 12 [1] => 42 [2] => 43 [3] => 98 [4] => 100 [5] => 100 )It’s important to note that key/value associations are not maintained. Consider the followingexample:$states = array("OH" => "Ohio", "CA" => "California", "MD" => "Maryland");sort($states);print_r($states);Here’s the output:Array ( [0] => California [1] => Maryland [2] => Ohio )To maintain these associations, use asort().Sorting an Array While Maintaining Key/Value PairsThe asort() function is identical to sort(), sorting an array in ascending order, except that thekey/value correspondence is maintained. Its prototype follows:void asort(array array [, integer sort_flags])Consider an array that contains the states in the order in which they joined the Union:$state[0] = "Delaware";$state[1] = "Pennsylvania";$state[2] = "New Jersey";Sorting this array using sort()produces the following ordering (note that the associative correlationare lost, which is probably a bad idea):Array ( [0] => Delaware [1] => New Jersey [2] => Pennsylvania )However, sorting with asort() produces the following:121

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!