Its prototype 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 )
CHAPTER 5 ?– ARRAYS 147
Sorting an Array
The 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,??? returning nothing, regardless of outcome. The optional sort_flags parameter
modifies the function??™s default behavior 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,
for instance. A quick search online produces several ASCII tables, so one isn??™t
reproduced in this book.
SORT_STRING: Sorts items in a fashion that might better correspond with how a
human might perceive the correct order. See natsort() for further information
about this matter, introduced later in this section.
Consider 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.
Pages:
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228