Yogesh Chauhan's Blog

6 Different Functions to Sort Arrays in PHP

in PHP on December 5, 2019

We just learned about different arrays in this blog post.

3 Types of Arrays in PHP

If PHP has 3 types of arrays, can we use one function to sort all those types of arrays?

The answer is NO.

We need different functions according to array type and according to the sorting type(ascending or descending). Let's look at all those functions one by one.

sort()

This function will sort indexed arrays in ascending order. For example:


sort(name of the array variable);

sort($hobbies); //checkout the DEMO
sort($numbers); //checkout the DEMO

rsort()

This function will sort indexed arrays in descending  order. For example:


rsort(name of the array variable);

rsort($hobbies); //checkout the DEMO
rsort($numbers); //checkout the DEMO

Let me explain associate arrays a bit here before we dive into sorting functions.


$age = array("Yogi"=>"28", "Hulk"=>"56", "Captain"=>"34");
asort($age);

In the associative array example above, "Yogi" is the key and "28" is the value. So, we can sort the whole array either by key or by value. Each sorting order has a different function.

asort()

This function will sort associative arrays in ascending order, according to the value. For example:


asort(name of the array variable); 

asort($age); //checkout the DEMO

ksort()

This function will sort associative arrays in ascending order, according to the key. For example:


ksort(name of the array variable); 

ksort($age); //checkout the DEMO

arsort()

This function will sort associative arrays in descending order, according to the value. For example:


arsort(name of the array variable); 

arsort($age); //checkout the DEMO

krsort()

This function will sort associative arrays in descending order, according to the key. For example:


krsort(name of the array variable); 

krsort($age); //checkout the DEMO

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to enable and disable button using JavaScript?JavaScriptHow to animate list items using CSS and JavaScript?CSSWhat is PostgreSQL? How similar or different it is from SQL?PostgresHow to convert an object from API to JSON array in Angular 9?AngularSome SQL LIKE Operators We Need to Keep in MindSQL/MySQLHow to add recaptcha version 3 to PHP website?PHP