Current Location: Home> Latest Articles> What’s the Difference Between krsort and uasort? A Complete Guide to PHP Sorting Functions and Use Cases

What’s the Difference Between krsort and uasort? A Complete Guide to PHP Sorting Functions and Use Cases

gitbox 2025-09-11

<?php
// Main content starts

echo "

What’s the Difference Between krsort and uasort? A Complete Guide to PHP Sorting Functions and Use Cases

";

// Introduction
echo "

In PHP development, array sorting is a very common operation. PHP provides a wide range of sorting functions, among which krsort and uasort are frequently used but often confused. This article explains their differences, use cases, and example code to help you use them more effectively in real-world development.

"
;

// 1. Introduction to krsort
echo "

1. Introduction to krsort

"
;
echo "

krsort is a built-in PHP array sorting function used to sort an array by its keys in descending order. It preserves the key-value association.

"
;
echo "

Syntax:

"
;
echo "
bool krsort(array &<span>$array</span></span></span><span>, int </span><span><span>$sort_flags</span></span><span> = SORT_REGULAR)
";
echo "

Parameters:

"
;
echo "

  • $array: The array to be sorted, passed by reference.
  • $sort_flags: Sorting behavior, such as SORT_NUMERIC, SORT_STRING, etc. Defaults to SORT_REGULAR.
";

// krsort example
echo "

Example:

"
;
echo "
<br>
$arr = ['b' => 2, 'a' => 1, 'c' => 3];<br>
krsort($arr);<br>
print_r($arr);<br>
"
;
echo "

Output:

"
;
echo "
<br>
Array<br>
(<br>
[c] => 3<br>
[b] => 2<br>
[a] => 1<br>
)<br>
"
;

// 2. Introduction to uasort
echo "

2. Introduction to uasort

"
;
echo "

uasort is another built-in PHP sorting function, but it sorts based on array values using a custom comparison function, while preserving the key-value association.

"
;
echo "

Syntax:

"
;
echo "
bool uasort(array &<span>$array</span></span></span><span>, callable </span><span><span>$callback</span></span><span>)
";
echo "

Parameters:

"
;
echo "

  • $array: The array to be sorted, passed by reference.
  • $callback: A user-defined comparison function that determines the sorting order. It accepts two arguments and should return -1, 0, or 1.
";

// uasort example
echo "

Example:

"
;
echo "
<br>
$arr = ['b' => 2, 'a' => 1, 'c' => 3];<br>
uasort($arr, function($x, $y) {<br>
return $y - $x; // Sort in descending order<br>
});<br>
print_r($arr);<br>
"
;
echo "

Output:

"
;
echo "
<br>
Array<br>
(<br>
[c] => 3<br>
[b] => 2<br>
[a] => 1<br>
)<br>
"
;

// 3. Differences Between krsort and uasort
echo "

3. Differences Between krsort and uasort

"
;
echo "

  • Sorting basis: krsort sorts by keys in descending order, while uasort sorts by values using a custom comparison function.
  • Flexibility: krsort does not require a callback, and its sorting rule is fixed. uasort requires a callback, allowing complex sorting logic.
  • Preservation of key-value association: Both functions preserve key-value relationships.
"
;

// 4. Use cases
echo "

4. Use Cases

"
;
echo "

  • krsort: Ideal for quickly sorting arrays by keys in descending order, such as dictionary or index reverse display.
  • uasort: Suitable when values need complex sorting, such as sorting by object properties, string length, or dates.
"
;

// Conclusion
echo "

Conclusion

"
;
echo "

Both krsort and uasort are highly useful sorting functions in PHP. Understanding their differences and use cases helps developers handle array data more efficiently and flexibly. Use krsort for sorting by keys in descending order, and uasort for custom value-based sorting. Choosing the right function makes code cleaner and easier to maintain.

"
;

?>