➤ sort() - sort arrays in ascending order.
$numbers = array(5, 1, 3, 4, 2);
sort($numbers);
print_r ($numbers); // outputs: 1 2 3 4 5
$cars = array("Ferrari", "BMW", "Mustang");
sort($cars);
print_r ($cars); // outputs: BMW Ferrari Mustang
// ASCII Code 66 - B
// ASCII Code 70 - F
// ASCII Code 77 - M
➤ rsort() - sort arrays in descending order.
$numbers = array(5, 1, 3, 4, 2);
rsort($numbers);
print_r ($numbers); // outputs: 5 4 3 2 1
➤ asort() - sort associative arrays in ascending order, according to the value.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
asort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
// Key = Keanu Reeves -> Value = 58
➤ ksort() - sort associative arrays in ascending order, according to the key.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
ksort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keanu Reeves -> Value = 58
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
• arsort() - sort associative arrays in descending order, according to the value.
• krsort() - sort associative arrays in descending order, according to the key.