PHP uksort() Function

What is PHP uksort() Function?

If you want to sort an array keys based on custom rules or/and based on multiple fields, use uksort() function. You can perform complex sorting or multidimensional array sorting with this function.

How PHP uksort() Function Works (Sorts)?

The uksort() function has a callback function (which is a user-defined comparison function) in its second parameter. It is the heart of the uksort() function as it determines the order of the elements’ keys of the array. The array is the first parameter of the uksort() function. The comparison function has two parameters which represents 2 keys from the array.

The comparison function returns one of the following 3 integers based on the comparison.  Let’s check how-

  1. 0: If both keys are equal.
  2. Greater than 0 (ex. 1): If 1st key is larger than the 2nd key.
  3. Less than 0 (ex.-1): If 1st key is smaller than the 2nd key.

Syntax:

uksort(array, callback)

Parameters:

The Function has 2 required parameters –

array (Required): it specifies an array.

callback (Required): It is a comparison function.

Return Values:

The function always returns TRUE.

Examples:

Example 1:

<pre>
<?php
function sortByDensity( $a, $b){   
    if ($a == $b)
        return 0;
    if ($a < $b)
        return -1;
    else
        return 1;
    }
    $density = array("New Jersey" => 1291, "Massachusetts" => 914, "Connecticut" => 758, "Maryland" => 645, "Delaware" => 539);
    uksort($density, "sortByDensity");
    print_r($density);
?>
</pre>

Output:

Array
(
[Connecticut] => 758
[Delaware] => 539
[Maryland] => 645
[Massachusetts] => 914
[New Jersey] => 1291
)

Example 2: Sorting a multidimensional array-

<pre>
<?php
function sortByPopulation( $a, $b){   
    if ($a == $b)
        return 0;
    if ($a < $b)
        return -1;
    else
        return 1;
    }
    $density = [
        "New Jersey" => ["Density" => 1291, "Population" => 9500000],
        "Massachusetts" => ["Density" => 914, "Population" => 7100000],
        "Connecticut" => ["Density" => 758, "Population" => 3600000],
        "Maryland" => ["Density" => 645, "Population" => 6200000],
        "Delaware" => ["Density" => 539, "Population" => 1000000]
    ];
    uksort($density, "sortByPopulation");
    print_r($density);
?>
</pre>

Output:

Array
(
[Connecticut] => Array
(
[Density] => 758
[Population] => 3600000
)

[Delaware] => Array
(
[Density] => 539
[Population] => 1000000
)

[Maryland] => Array
(
[Density] => 645
[Population] => 6200000
)

[Massachusetts] => Array
(
[Density] => 914
[Population] => 7100000
)

[New Jersey] => Array
(
[Density] => 1291
[Population] => 9500000
)
)

Notes on uksort() Function:

  • If multiple elements have same values, the function doesn’t change their order.
  • The function reindexes the keys of the sorted array starting with 0. So, it doesn’t preserve the keys of the original array.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP uksort() Function

The uksort() function is a built-in PHP function and part of the PHP’s array functions. It is a very powerful sorting function that allows you to sort array keys according to your conditions.

Reference:

https://www.php.net/manual/en/function.uksort.php