Get your next remote job on LaraJobs.
PHP

Learn how to sort any kind of array in PHP

Benjamin Crozat
Published on Nov 9, 2023 0 comments Edit on GitHub
Learn how to sort any kind of array in PHP

Sorting arrays is a common task in PHP, and the language provides a variety of functions to order elements just the way you need.

Whether you’re dealing with numerical indices or associative arrays, PHP has you covered. 😎

Let me show you some of PHP’s array sorting capabilities.

Utilizing the sort() and rsort() Functions

When you have a simple indexed array that needs reordering, sort() and rsort() are the straightforward choices for ascending and descending order, respectively.

For sort():

$fruits = ["Banana", "Apple", "Orange"];

sort($fruits);

var_dump($fruits);

Once sorted, the $fruits array will be:

["Apple", "Banana", "Orange"]

Now, what to do when you have an associative array? The sort() function won’t cut it.

Maintaining key association with asort() and arsort()

Associative arrays require maintaining the relationship between keys and values. asort() sorts in ascending order by value, maintaining key association, and arsort() does the same in descending order.

Example of asort():

$prices = [
    "Apple" => 1.2,
    "Banana" => 0.5,
    "Orange" => 0.9,
];

asort($prices);

var_dump($prices);

The sorted $prices array now looks like this:

[
    "Banana" => 0.5,
    "Orange" => 0.9,
    "Apple" => 1.2,
]

Key-centric sorting with ksort() and krsort()

To sort an associative array by its keys, ksort() for ascending order and krsort() for descending order are your go-to functions.

Using ksort():

$products = [
    "product3" => "Chair",
    "product1" => "Desk",
    "product2" => "Lamp",
];

ksort($products);

var_dump($products);

Custom sorting with usort(), uasort(), and uksort()

What we saw until now will probably cover 99% of your needs.

But sometimes, you need a sorting logic that’s not built-in. For these instances, usort() for value-based, uasort() for value-based with preserved keys, and uksort() for key-based custom sorting. Each requires a user-defined comparison function.

An example with usort():

$numbers = [3, 2, 5, 6, 1];

usort($numbers, function ($a, $b) {
    return $a <=> $b; // The spaceship operator
});

var_dump($numbers);

With the custom function, $numbers will be sorted as:

[1, 2, 3, 5, 6];

Of course, in that case, using sort() would be better. I just wanted to let you know that custom sorting logic is possible! 🙂

Wait, there's more!

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • ZoneWatcher
    Get instant alerts on DNS changes across all major providers, before your customers notice.
    25% off for 12 months using the promo code CROZAT.
    Try ZoneWatcher for free
  • Quickly build highly customizable admin panels for Laravel projects.
    20% off on the pro version using the promo code CROZAT.
    Try Backpack for free
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -