PHP function to sort array alphabetically or numerically in ascending or descending order.
October 16, 2016 Comments 2 182 Views

PHP function to sort array alphabetically or numerically in ascending or descending order.

In this article we will walk through with example on how the PHP array sort functions works to sort the arrays key and values in alphabetically/numerically or ascending/descending order.

Sort using PHP built-in Sort Functions :

  • sort() – PHP function to sort arrays in ascending order
  • rsort() – PHP function to sort arrays in descending order
  • asort() – PHP function to sort associative arrays in ascending order with reference to value
  • ksort() – PHP function to sort associative arrays in ascending order with reference to key
  • arsort() – PHP function to sort associative arrays in descending order with reference to value
  • krsort() – PHP function to sort associative arrays in descending order with reference to key

PHP function to sort Array in Ascending Order – sort()

The following example sorts the elements of the $fruits array in ascending alphabetical order:

Alphabetical Ordering :

Numerical Ordering :

PHP function to sort Array in Descending Order – rsort()

The following example sorts the elements of the $fruits array in descending alphabetical order:

PHP function to sort Array (Ascending Order), with reference to Value using – asort()

The following example sorts an associative array in ascending order by array value:

PHP function to sort Array (Ascending Order), According to Key – ksort()

The following example sorts an associative array in ascending order, according to the key:

PHP function to sort Array (Descending Order), According to Value – arsort()

The following example sorts an associative array in descending order, according to the value:

PHP function to sort Array (Descending Order), According to Key – krsort()

The following example sorts an associative array in descending order, according to the key:

Previous Creating HTML Form with PHP Server Side Validation.
Next Boost your wordpress site using htaccess optimization methods

About author

Rio
Rio 34 posts

Expert web developer working in PHP, Wordpress, Joomla, Magento, Javascript etc.

You might also like

Working With PHP & Databases

Working with PHP & Database require basic knowledge of below items : Database Connection Inserting Data Reading Data Updating Data Deleting Data Making Database Connection : Using PHP you can

Using Type Declarations in PHP 7

PHP is considered to be a weak typed language. Using type declarations in PHP 7 simply means specifying which type of variable is being set instead of allowing PHP to

How to detect website is access from Mobile using PHP.

Todays trend shows that maximum number of website access using mobile is increased. People are using smart phone to access the mobile instead of their desktop OR laptop. Because mobile

Secure directory by allowing access to IP address & Deny all using htaccess

Your website may have folders which you want to keep secure access using URL. For example Admin area you want to keep secure to access using particular IP address and

Error Handling in PHP 7

The next feature we going to cover are the changes to Error Handling. Handling fatal errors in the past has been next to impossible in PHP. A fatal error would

PHP 7 Using New Operators

PHP 7 also brings us some new operators. Let’s just cut the talk and directly check how PHP 7 Using New Operators. Spaceship Operator : The spaceship operator, or Combined Comparison

Post data to third party site using PHP CURL

PHP supports CURL library which allows you to connect and communicate to many different types of servers with many different types of protocols and supports the http, https, ftp etc.

Working with PHP Array – Indexed, Associative & Multi-Dimensional

PHP Array are variables which can hold more than one value at a time. For example if you want to store 1000 numbers then instead of defining 1000 variables its easy

Creating HTML Form with PHP Server Side Validation.

This article will explain how you can create a HTML Form and do server side validation for the form. So the form which will display will be plain HTML and

2 Comments

  1. Manny
    October 27, 12:17 Reply
    Great explanation. Thank you the rsort was the function i was looking for. It helps me :-)
  2. Kinsa Rumel
    October 27, 12:19 Reply
    Thank you for the good examples. It was easy to understand and I have clear idea of array sorting now.

Leave a Reply

two × four =