---
title: "Sorting of an array in PHP"  
description: "In PHP there are some sorting function .These function are used to sort the array.  sort() :This function is used to sort in ascending alphabetical or"  
author: "zack mathews"  
published: 2016-06-06  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11141/sorting-of-an-array-in-php  
category: "php"  
tags: ["php", "array", "sorting"]  
reading_time: 2 minutes  

---

# Sorting of an array in PHP

In PHP there are some [sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) .These function are used to sort the array.\

##### sort() :

This function is used to sort in **ascending alphabetical** order.

```
<?php$mobile = array("Samsung", "Nokia", "Micromax");sort($mobile);
$length = count($mobile);for($a = 0; $a < $length; $a++) {
    echo $mobile[$a];    echo "<br>";}?>
```

[Output](https://www.mindstick.com/forum/161319/how-does-the-print-function-handle-output-in-python):

Micromax

Nokia

Samsung

##### rsort():

This function is used to sort in **descending alphabetical** order.\

```
<?php$mobile = array("Samsung", "Nokia", "Micromax");rsort($mobile);$length = count($mobile);for($a = 0; $a < $length; $a++) {
    echo $mobile[$a];    echo "<br>";}?>
```

Output:

Samsung

Nokia

Micromax

##### asort():

Sort an associative [array in ascending order](https://www.mindstick.com/forum/33695/sorting-mutable-array-in-ascending-order-in-objective-c), according to the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages).

```
<?php$salary = array("Neha"=>"35000", "Shikha"=>"45000", "Ravi"=>"40000");asort($salary);
foreach($salary as $a => $a_value) {    echo "Key=" . $a . ", Value=" . $a_value;    echo "<br>";}
?>
```

Output:

Key=Neha, Value=35000

Key=Ravi, Value=40000

Key=Shikha, Value=45000

##### ksort():

Sort an [associative array](https://www.mindstick.com/forum/606/length-of-javascript-object-ie-associative-array) in ascending order, according to the key.\

```
<?php$salary = array("Shikha"=>"45000", "Neha"=>"35000", "Ravi"=>"40000");ksort($salary);
foreach($salary as $a => $a_value) {    echo "Key=" . $a . ", Value=" . $a_value;    echo "<br>";}
?>
```

Output:

Key=Neha, Value=35000

Key=Ravi, Value=40000

Key=Shikha, Value=45000

##### arsort():

Sort an associative array in [descending order](https://www.mindstick.com/forum/160082/how-to-sort-the-results-of-an-sql-search-query-in-ascending-and-descending-order), according to the key.

```
<?php$salary = array("Shikha"=>"45000", "Neha"=>"35000", "Ravi"=>"40000");arsort($salary);
foreach($salary as $a => $a_value) {    echo "Key=" . $a . ", Value=" . $a_value;    echo "<br>";}?>
```

Output:

Key=Shikha, Value=45000

Key=Ravi, Value=40000

Key=Neha, Value=35000

##### krsort():

Sort an associative array in **descending** order, according to the key.

```
<?php$salary = array("Shikha"=>"45000", "Neha"=>"35000", "Ravi"=>"40000");krsort($salary);
foreach($salary as $a => $a_value) {    echo "Key=" . $a . ", Value=" . $a_value;    echo "<br>";}
?>
```

**Output:**

Key=Shikha, Value=45000

Key=Ravi, Value=40000

Key=Neha, Value=35000

\

---

Original Source: https://www.mindstick.com/blog/11141/sorting-of-an-array-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
