11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5 • ARRAYSwww.it-ebooks.infoIf the optional sort_flags parameter is included, the exact sorting behavior is determined by itsvalue, as described in the sort() section. Keep in mind that the behavior will be applied to key sortingbut not to value sorting.Sorting Array Keys in Reverse OrderThe krsort() function operates identically to ksort(), sorting by key, except that it sorts in reverse(descending) order. Its prototype follows:integer krsort(array array [, int sort_flags])Sorting According to User-Defined CriteriaThe usort() function offers a means for sorting an array by using a user-defined comparison algorithm,embodied within a function. This is useful when you need to sort data in a fashion not offered by one of<strong>PHP</strong>’s built-in sorting functions. Its prototype follows:void usort(array array, callback function_name)The user-defined function must take as input two arguments and must return a negative integer,zero, or a positive integer, respectively, based on whether the first argument is less than, equal to, orgreater than the second argument. Not surprisingly, this function must be made available to the samescope in which usort() is being called.A particularly applicable example of where usort() comes in handy involves the ordering ofAmerican-format dates (month, day, year, as opposed to day, month, year used by most othercountries). Suppose that you want to sort an array of dates in ascending order. While you might think thesort() or natsort() functions are suitable for the job, as it turns out, both produce undesirable results.The only recourse is to create a custom function capable of sorting these dates in the correct ordering:

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

Saved successfully!

Ooh no, something went wrong!