21.2. Built-In Sorting Functions

Usually, it will not be necessary to write your own sort functions. PHP offers several functions for sorting arrays. The most basic is sort. This function is described, along with the other sorting functions in Chapter 11. It's instructive to compare sort to rsort, asort, and ksort.

The sort function puts all the elements in the array in order from lowest to highest. If the array contains any strings, then this means ordering them by the ASCII codes of each character. If the array contains only numbers, then they are ordered by their values. The indices—the values used to reference the elements—are discarded and replaced with integers starting with zero. This is an important effect, which Listing 21.1 demonstrates; ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.