PHP Sorting an Array


Example

There are several sort functions for arrays in php:

sort()

Sort an array in ascending order by value.

$fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel'];
sort($fruits);
print_r($fruits);

results in

Array
(
    [0] => Apfel
    [1] => Banane
    [2] => Orange
    [3] => Zitrone
)

rsort()

Sort an array in descending order by value.

$fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel'];
rsort($fruits);
print_r($fruits);

results in

Array
(
    [0] => Zitrone
    [1] => Orange
    [2] => Banane
    [3] => Apfel
)

asort()

Sort an array in ascending order by value and preserve the indecies.

$fruits = [1 => 'lemon', 2 => 'orange',  3 => 'banana', 4 => 'apple'];
asort($fruits);
print_r($fruits);

results in

Array
(
    [4] => apple
    [3] => banana
    [1] => lemon
    [2] => orange
)

arsort()

Sort an array in descending order by value and preserve the indecies.

$fruits = [1 => 'lemon', 2 => 'orange',  3 => 'banana', 4 => 'apple'];
arsort($fruits);
print_r($fruits);

results in

Array
(
    [2] => orange
    [1] => lemon
    [3] => banana
    [4] => apple
)

ksort()

Sort an array in ascending order by key

$fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple'];
ksort($fruits);
print_r($fruits);

results in

Array
(
    [a] => orange
    [b] => banana
    [c] => apple
    [d] => lemon
)

krsort()

Sort an array in descending order by key.

$fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple'];
krsort($fruits);
print_r($fruits);

results in

Array
(
    [d] => lemon
    [c] => apple
    [b] => banana
    [a] => orange
)

natsort()

Sort an array in a way a human being would do (natural order).

$files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack'];
natsort($files);
print_r($files);

results in

Array
(
    [4] => File2.stack
    [0] => File8.stack
    [2] => file7.stack
    [3] => file13.stack
    [1] => file77.stack
)

natcasesort()

Sort an array in a way a human being would do (natural order), but case intensive

$files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack'];
natcasesort($files);
print_r($files);

results in

Array
(
    [4] => File2.stack
    [2] => file7.stack
    [0] => File8.stack
    [3] => file13.stack
    [1] => file77.stack
)

shuffle()

Shuffles an array (sorted randomly).

$array = ['aa', 'bb', 'cc'];
shuffle($array);
print_r($array);

As written in the description it is random so here only one example in what it can result

Array
(
    [0] => cc
    [1] => bb
    [2] => aa
)

usort()

Sort an array with a user defined comparison function.

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

$array = [3, 2, 5, 6, 1];
usort($array, 'compare');
print_r($array);

results in

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 6
)

uasort()

Sort an array with a user defined comparison function and preserve the keys.

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

$array = ['a' => 1, 'b' => -3, 'c' => 5, 'd' => 3, 'e' => -5];
uasort($array, 'compare');
print_r($array);

results in

Array
(
    [e] => -5
    [b] => -3
    [a] => 1
    [d] => 3
    [c] => 5
)

uksort()

Sort an array by keys with a user defined comparison function.

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

$array = ['ee' => 1, 'g' => -3, '4' => 5, 'k' => 3, 'oo' => -5];

uksort($array, 'compare');
print_r($array);

results in

Array
(
    [ee] => 1
    [g] => -3
    [k] => 3
    [oo] => -5
    [4] => 5
)