Prev | Current Page 219 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

jpg,
picture10.jpg, picture20.jpg. Sorting these items using typical algorithms results in
the following ordering:
picture1.jpg, picture10.jpg, picture2.jpg, picture20.jpg
Certainly not what you might have expected, right? The natsort() function resolves
this dilemma, sorting the array in the order you would expect, like so:
picture1.jpg, picture2.jpg, picture10.jpg, picture20.jpg
Case-Insensitive Natural Sorting
The function natcasesort() is functionally identical to natsort(), except that it is
case insensitive:
void natcasesort(array array)
Returning to the file-sorting dilemma raised in the natsort() section, suppose
that the pictures are named like this: Picture1.JPG, picture2.jpg, PICTURE10.jpg,
picture20.jpg. The natsort() function would do its best, sorting these items like so:
PICTURE10.jpg, Picture1.JPG, picture2.jpg, picture20.jpg
The natcasesort() function resolves this idiosyncrasy, sorting as you might expect:
CHAPTER 5 ?–  ARRAYS 151
Picture1.jpg, PICTURE10.jpg, picture2.jpg, picture20.jpg
Sorting an Array by Key Values
The ksort() function sorts an array by its keys, returning TRUE on success and FALSE
otherwise. Its prototype follows:
integer ksort(array array [, int sort_flags])
If the optional sort_flags parameter is included, the exact sorting behavior is
determined by its value, as described in the sort() section. Keep in mind that the
behavior will be applied to key sorting but not to value sorting.


Pages:
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231