PHP Array Sort Functions Code Example for Sorting by Key or Value

PHP Array Sorting

PHP Array Sorting means to arrange the elements of the array in a particular(ascending or descending or alphabetically) order. PHP has some array sorting functions to sort the array.

PHP Array Sorting Functions

PHP have some built-in sorting functions for array and these functions are used to sort the elements of the array.

List of popular pre-defined Array Sorting Functions

Fuctions Description
sort() To sort an array in ascending order.
rsort() To sort an array in descending order.
asort() To sort an associative array in ascending order on the basis of values.
ksort() To sort an associative array in ascending order on the basis of key.
arsort() To sort an associative arrya in descending order on the basis of values.
krsort() To sort an associative arrya in descending order on the basis of key.

sort() Function – To sort array in ascending order.

sort() function is used to sort an array in the ascending order.

Example 1 – sort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Maths", "Physics", "Chemistry", "English");
		echo $marks[0] . "<br />" . $marks[1] . "<br />" . $marks[2] . "<br />" . $marks[3] . "<br />";
		echo "<br>";
		sort($marks);
		echo "The sorted array....<br />";
		$arraylength = count($marks);
		for($i = 0; $i < $arraylength; $i++) 
		{
			echo $marks[$i];
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the sort() is used to sort an array.

PHP Array sort Function Example code
Fig.1 – PHP Array sort Function Example Code.

rsort() Function – To sort array in descending order.

The rsort() function is used to sort an array in the descending order.

Example 2 – rsort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Maths", "Physics", "Chemistry", "English");
		echo $marks[0] . "<br />" . $marks[1] . "<br />" . $marks[2] . "<br />" . $marks[3] . "<br />";
		echo "<br>";
		rsort($marks);
		echo "The sorted array....<br />";
		$arraylength = count($marks);
		for($i = 0; $i < $arraylength; $i++) 
		{
			echo $marks[$i];
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the rsort() is used to sort an array in the descending order.

Array rsort Function Example
Fig.2 – PHP Array rsort Function.

PHP asort() Function – To sort array in ascending order by values

The asort() function is used to sort an associative array in the ascending order but the array will be sort on the basis of “values”.

Example 3 – asort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Peter" => 75, "John" => 78, "Charlie" => 99);
		echo $marks["Peter"] . "<br />" . $marks["John"] . "<br />" . $marks["Charlie"] . "<br />";
		echo "<br>";
		asort($marks);
		echo "The sorted array....<br />";
		foreach($marks as $i => $i_value ) 
		{
			echo "Key=" . $i . ", Value=" . $i_value;
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the asort() is used to sort an array in the ascending order by their values.

Array asort Function
Fig.3 – PHP Array asort Function example code.

PHP ksort() Function – To sort array in ascending order by key

The ksort() function is used to sort an associative array in the ascending order but the array will be sort on the basis of “key”.

Example 4 – ksort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Peter" => 75, "John" => 78, "Charlie" => 99);
		foreach ($marks as $key => $value)
        {
            echo $key . "-" . $value . "<br />";
        }
		echo "<br>";
		ksort($marks);
		echo "The sorted array....<br />";
		foreach($marks as $key => $value ) 
		{
			echo $key . "-" . $value;
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the ksort() is used to sort an array in the ascending order by their key.

PHP Array ksort Function
Fig.4 – PHP Array ksort Function.

arsort() Function – To sort associative array in descending order on the basis of “values”

The arsort() function is used to sort an associative array in the descending order but the array will be sort on the basis of “values”.

Example 5 – arsort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Peter" => 75, "John" => 78, "Charlie" => 99);
		foreach ($marks as $key => $value)
        {
            echo $key . "-" . $value . "<br />";
        }
		echo "<br>";
		arsort($marks);
		echo "The sorted array....<br />";
		foreach($marks as $key => $value ) 
		{
			echo $key . "-" . $value;
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the arsort() is used to sort an array in the descending order by their key.

PHP Array Sort by value
Fig.5 – PHP Array Sort by value.

krsort() Function – To sort associative array in descending order on the bassis of “Key”.

The krsort() function is used to sort an associative array in the descending order but the array will be sort on the basis of “key”.

Example 6 – krsort() Function Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array("Peter" => 75, "John" => 78, "Charlie" => 99);
		foreach ($marks as $key => $value)
        {
            echo $key . "-" . $value . "<br />";
        }
		echo "<br>";
		krsort($marks);
		echo "The sorted array....<br />";
		foreach($marks as $key => $value ) 
		{
			echo $key . "-" . $value;
			echo "<br>";
		}
	?>
  </body>
</html>

In the above example, we show how the krsort() is used to sort an array in the descending order by their key.

PHP Array Sort by key
Fig.6 – PHP Array Sort by key with krsort().
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape