Prev | Current Page 215 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

PHP facilitates the process by offering a multitude of useful functions
capable of sorting arrays in a variety of manners. Those functions are introduced in
this section.
?– Tip By default, PHP??™s sorting functions sort in accordance with the rules as specified by the English
language. If you need to sort in another language, say French or German, you??™ll need to modify this
default behavior by setting your locale using the setlocale() function.
146 CHAPTER 5 ?–  ARRAYS
Reversing Array Element Order
The array_reverse() function reverses an array??™s element order. Its prototype follows:
array array_reverse(array array [, boolean preserve_keys])
If the optional preserve_keys parameter is set to TRUE, the key mappings are maintained.
Otherwise, each newly rearranged value will assume the key of the value
previously presiding at that position:
$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 in this case.
Flipping Array Keys and Values
The array_flip() function reverses the roles of the keys and their corresponding
values in an array.


Pages:
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227