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.

CHAPTER 5 • ARRAYSwww.it-ebooks.info$states = array("Delaware", "Pennsylvania", "New Jersey");print_r(array_reverse($states));// Array ( [0] => New Jersey [1] => Pennsylvania [2] => Delaware )Contrast this behavior with that resulting from enabling preserve_keys:$states = array("Delaware", "Pennsylvania", "New Jersey");print_r(array_reverse($states,1));// Array ( [2] => New Jersey [1] => Pennsylvania [0] => Delaware )Arrays with associative keys are not affected by preserve_keys; key mappings are always preserved inthis case.Flipping Array Keys and ValuesThe array_flip() function reverses the roles of the keys and their corresponding values in an array. Itsprototype follows:array array_flip(array array)An example follows:$state = array("Delaware", "Pennsylvania", "New Jersey");$state = array_flip($state);print_r($state);This example returns the following:Array ( [Delaware] => 0 [Pennsylvania] => 1 [New Jersey] => 2 )Sorting an ArrayThe sort() function sorts an array, ordering elements from lowest to highest value. Its prototype follows:void sort(array array [, int sort_flags])The sort() function doesn’t return the sorted array. Instead, it sorts the array “in place,” returningnothing, regardless of outcome. The optional sort_flags parameter modifies the function’s defaultbehavior in accordance with its assigned value:SORT_NUMERIC: Sorts items numerically. This is useful when sorting integers or floats.SORT_REGULAR: Sorts items by their ASCII value. This means that B will come before a, forinstance. A quick search online produces several ASCII tables, so one isn’t reproducedin this book.SORT_STRING: Sorts items in a fashion that better corresponds with how a human mightperceive the correct order. See natsort() for more information about this matter,introduced later in this section.120

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

Saved successfully!

Ooh no, something went wrong!