0
0
PHPprogramming~5 mins

Array sort functions in PHP

Choose your learning style9 modes available
Introduction

Sorting arrays helps organize data so it's easier to find or use. PHP provides simple functions to sort arrays in different ways.

When you want to list names in alphabetical order.
When you need to arrange numbers from smallest to largest.
When you want to sort data before displaying it on a webpage.
When you want to sort an array but keep the original keys.
When you want to sort arrays in reverse order.
Syntax
PHP
<?php
// Sort array values in ascending order and reindex keys
bool sort(array &$array)

// Sort array values in descending order and reindex keys
bool rsort(array &$array)

// Sort array by keys in ascending order
bool ksort(array &$array)

// Sort array by keys in descending order
bool krsort(array &$array)

// Sort array and maintain key association in ascending order
bool asort(array &$array)

// Sort array and maintain key association in descending order
bool arsort(array &$array)
?>

All these functions change the original array directly (in-place).

They return true on success and false on failure.

Examples
Sorts values alphabetically and resets keys to 0,1,2...
PHP
<?php
$fruits = ['banana', 'apple', 'cherry'];
sort($fruits);
print_r($fruits);
Sorts array by keys in ascending order, keys stay the same.
PHP
<?php
$numbers = [3 => 10, 1 => 5, 2 => 7];
ksort($numbers);
print_r($numbers);
Sorts by values ascending but keeps the original keys.
PHP
<?php
$prices = ['apple' => 3, 'banana' => 1, 'cherry' => 2];
asort($prices);
print_r($prices);
Sorting an empty array does nothing and is safe.
PHP
<?php
$empty = [];
sort($empty);
print_r($empty);
Sample Program

This program shows how sorting changes the array order and keys. It prints the array before and after sorting with different functions.

PHP
<?php
// Create an array of fruits with keys
$fruits = [2 => 'banana', 0 => 'apple', 1 => 'cherry'];

// Print original array
echo "Original array:\n";
print_r($fruits);

// Sort values ascending and reset keys
sort($fruits);
echo "\nAfter sort():\n";
print_r($fruits);

// Create an associative array of prices
$prices = ['apple' => 3, 'banana' => 1, 'cherry' => 2];

// Print original prices
echo "\nOriginal prices:\n";
print_r($prices);

// Sort prices by value ascending, keep keys
asort($prices);
echo "\nAfter asort():\n";
print_r($prices);

// Sort prices by key descending
krsort($prices);
echo "\nAfter krsort():\n";
print_r($prices);
?>
OutputSuccess
Important Notes

Sorting functions usually run in O(n log n) time, which is fast for most uses.

They change the original array, so if you need the original order, copy the array first.

Common mistake: expecting keys to stay the same with sort() -- it resets keys.

Use asort() or arsort() if you want to keep keys with sorted values.

Summary

PHP has many array sort functions to order values or keys.

sort() resets keys, asort() keeps keys.

Sorting helps organize data for easier use or display.