There are several sort functions for arrays in php:
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
)
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
)
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
)
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