Common Sorting Functions in PHP: sort, rsort, asort, ksort, and uasort

This article introduces the most frequently used PHP sorting functions—including sort, rsort, asort, ksort, and uasort—explaining their behavior, usage examples, and output to help developers choose the appropriate function for arrays and associative arrays.

php Courses
php Courses
php Courses
Common Sorting Functions in PHP: sort, rsort, asort, ksort, and uasort

Sorting is a common operation in PHP programming, and the language provides a variety of built‑in functions to sort arrays, strings, and other data structures. This article explains the most frequently used sorting functions, their behavior, and how to apply them.

sort() function

The sort() function sorts an indexed array in ascending order, modifying the original array. Example: $arr = array(5, 3, 8, 2, 9); After calling sort($arr); the array becomes [2, 3, 5, 8, 9].

rsort() function

The rsort() function sorts an indexed array in descending order, also modifying the original array. Example: $arr = array(5, 3, 8, 2, 9); After rsort($arr); the result is [9, 8, 5, 3, 2].

asort() function

The asort() function sorts an associative array by its values in ascending order while preserving key‑value associations. Example:

$arr = array("Tom" => 80, "Jerry" => 90, "Alice" => 75);

Calling asort($arr); yields ["Alice" => 75, "Tom" => 80, "Jerry" => 90].

ksort() function

The ksort() function sorts an associative array by its keys in ascending order, keeping the key‑value pairs intact. Example:

$arr = array("Tom" => 80, "Jerry" => 90, "Alice" => 75);

After ksort($arr); the array becomes ["Alice" => 75, "Jerry" => 90, "Tom" => 80].

uasort() function

The uasort() function sorts an associative array by its values using a user‑defined comparison function. Example comparison function (descending order):

function compare($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

Applying uasort($arr, "compare"); to the same student‑score array results in ["Jerry" => 90, "Tom" => 80, "Alice" => 75].

Conclusion

PHP offers a rich set of sorting functions that can handle both indexed and associative arrays, with options for ascending, descending, and custom ordering. Choosing the right function improves code efficiency and readability, making sorting an essential skill for PHP developers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPfunctions
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.