Free cookie consent management tool by TermsFeed Generator PHP Array Sort | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Sort

PHP Array Sort

Last Updated on Feb 14, 2023

Introduction

If you want to sort an array in PHP there are 3 factors you should have in mind:

  1. do you want to sort by value or by the key?
  2. what is the order of your sort? Natural, descending , ascending or user defined?
  3. do you want to keep the key association? Or do you want to remove the keys and reassign them with new indexes?

So let’s get started with some of the most useful and common array sort functions in PHP:

Original Array

We have this array and we want to sort it:

$array = [
   'twitter' => 'pratham',
   'feedhive' => 'simon',
   'php' => 'amir',
   'oliver',
   'vitto',
   'hassib'
];
//Array (
//      [twitter] => pratham
//      [feedhive] => simon
//      [php] => amir
//      [0] => oliver
//      [1] => vitto
//      [2] => hassib
// )

As you can see we have 3 items that have string as key and 3 item that have numbers as key.

Before we start, note that sort functions that we are going to see, sort the original array. They don’t return a new array.

Now let’s get started.

sort

Sorts the array based on values

It sorts the values in ascending order

It removes the keys and reindexes the array

sort($array);
print_r($array);
//Array
//(
//   [0] => amir
//   [1] => hassib
//   [2] => oliver
//   [3] => pratham
//   [4] => simon
//   [5] => vitto
//)

rsort

Like sort but it sorts the values in descending order

rsort($array);
print_r($array);
//Array
//(
//  [0] => vitto
//  [1] => simon
//  [2] => pratham
//  [3] => oliver
//  [4] => hassib
//  [5] => amir
//)

ksort

Sorts the array based on keys

It sorts the keys in ascending order

So obviously it will keep the keys and won’t remove them

ksort($array);
print_r($array);
//Array
//(
//    [feedhive] => simon
//    [php] => amir
//    [twitter] => pratham
//    [0] => oliver
//    [1] => vitto
//    [2] => hassib
//)

krsort

It’s like ksort but it sorts the keys ins descending order

krsort($array);
print_r($array);
//Array
//(
//    [2] => hassib
//    [1] => vitto
//    [twitter] => pratham
//    [php] => amir
//    [feedhive] => simon
//    [0] => oliver
//)

asort

Sorts the array based on values

It sorts the values in ascending order

It keeps the original keys and won’t remove them

asort($array);
print_r($array);
//Array
//(
//    [php] => amir
//    [2] => hassib
//    [0] => oliver
//    [twitter] => pratham
//    [feedhive] => simon
//    [1] => vitto
//)

arsort

Like asort but sorts the values in descending order

arsort($array);
print_r($array);
//Array
//(
//    [1] => vitto
//    [feedhive] => simon
//    [twitter] => pratham
//    [0] => oliver
//    [2] => hassib
//    [php] => amir
//)

Conclusion

Now you know about array sort functions in PHP.

I recommend you to open a PHP files and try to sort an array with all the functions we learnt.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • sort an array in PHP
  • sort function
  • rsort function
  • asrot function
  • arsort function
  • ksort function
  • krsort function
  • sort functions comparison

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
Creating a Simple Template Engine like Laravel Blade in PHP
Aug 21, 2023 programming

Creating a Simple Template Engine like Laravel Blade in PHP

Today we are going to learn how to build a simple template engine like Laravel's Blade. it's a great learning opportunity to understand how popular engines like Blade might work under the hood. ...

4 Min Read Read More
How to Combine Excel Files Using PHP
May 06, 2024 programming

How to Combine Excel Files Using PHP

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with PHP can be easily done. ...

11 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

38 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...