Array Sorting
×
Home Courses Community Support
Array Sorting

In PHP, we have some sort functions to sort array. By using these functions you can use manage array as per your requirement.

Let's see an array without shorting 

<?php
// array $emp with five employee name
$emp = array("Anil", "Supriya", "Deepak", "Kaushal", "Divya");

// Print employee names from array 
echo $emp[0].", ".$emp[1].", ".$emp[2].", ".$emp[3].", ".$emp[4];
?>

Output:

Anil, Supriya, Deepak, Kaushal, Divya

Function sort()

sort() function is used to sort the array data in ascending order. Let's apply sort() function on above mentioned array, it will short the employee names.

<?php
// array $emp with five employee name
$emp = array("Anil", "Supriya", "Deepak", "Kaushal", "Divya");

//sort the array using sort function
sort ($emp);

// Print employee names from array 
echo $emp[0].", ".$emp[1].", ".$emp[2].", ".$emp[3].", ".$emp[4];
?>

Output:

Anil,& Deepak, Divya, Kaushal, Supriya


Function rsort()

This function sort the array in decending order. Let's see the example-

<?php
// array $emp with five employee name
$emp = array("Anil", "Supriya", "Deepak", "Kaushal", "Divya");

//sort the array using rsort function
rsort ($emp);

// Print employee names from array 
echo $emp[0].", ".$emp[1].", ".$emp[2].", ".$emp[3].", ".$emp[4];
?>

Output:

Supriya, Kaushal, Divya, Deepak, Anil


Function asort()

By using this function we sort the associative array in ascending order, according to it's value. See below example without sorting -

<?php
// array for salary of five employees
$salary = array('Anil' => "70,000", 'Supriya' => "65,000", 'Deepak' => "50,000", 'Kaushal' => "45,000", 'Divya' => "40,000");

//Print array data
var_dump($salary);
?>

 

Output:
array (size=5)
  'Divya' => string '40,000' (length=6)
  'Kaushal' => string '45,000' (length=6)
  'Deepak' => string '50,000' (length=6)
  'Supriya' => string '65,000' (length=6)
  'Anil' => string '70,000' (length=6)


Now we will sort it by using asort() function. 

<?php
// array for salary of five employees
$salary = array('Anil' => "70,000", 'Supriya' => "65,000", 'Deepak' => "50,000", 'Kaushal' => "45,000", 'Divya' => "40,000");

//sort the array using asort function
asort ($salary);

//Print array data
var_dump($salary);
?>

 

Output:

array (size=5)
  'Divya' => string '40,000' (length=6)
  'Kaushal' => string '45,000' (length=6)
  'Deepak' => string '50,000' (length=6)
  'Supriya' => string '65,000' (length=6)
  'Anil' => string '70,000' (length=6)


Function ksort()

By using this function we sort the associative array in ascending order, according to it's key.

See below example -

<?php
// array for salary of five employees
$salary = array('Anil' => "70,000", 'Supriya' => "65,000", 'Deepak' => "50,000", 'Kaushal' => "45,000", 'Divya' => "40,000");

//sort the array using ksort function
ksort ($salary);

//Print array data
var_dump($salary);
?>

 

 Output:

array (size=5)
  'Anil' => string '70,000' (length=6)
  'Deepak' => string '50,000' (length=6)
  'Divya' => string '40,000' (length=6)
  'Kaushal' => string '45,000' (length=6)
  'Supriya' => string '65,000' (length=6)


Function arsort()

By using this function we sort the associative array in decending order, according to it's value.

See below example - 

<?php
// array for salary of five employees
$salary = array('Anil' => "70,000", 'Supriya' => "65,000", 'Deepak' => "50,000", 'Kaushal' => "45,000", 'Divya' => "40,000");

//sort the array using arsort function
arsort ($salary);

//Print array data
var_dump($salary);
?>

 

Output:

array (size=5)
  'Anil' => string '70,000' (length=6)
  'Supriya' => string '65,000' (length=6)
  'Deepak' => string '50,000' (length=6)
  'Kaushal' => string '45,000' (length=6)
  'Divya' => string '40,000' (length=6)


Function krsort()

By using this function we sort the associative array in decending order, according to it's key.

See below example -

<?php
// array for salary of five employees
$salary = array('Anil' => "70,000", 'Supriya' => "65,000", 'Deepak' => "50,000", 'Kaushal' => "45,000", 'Divya' => "40,000");

//sort the array using krsort function
krsort ($salary);

//Print array data
var_dump($salary);
?>

 

Output:

array (size=5)
  'Supriya' => string '65,000' (length=6)
  'Kaushal' => string '45,000' (length=6)
  'Divya' => string '40,000' (length=6)
  'Deepak' => string '50,000' (length=6)
  'Anil' => string '70,000' (length=6)

 

 

← Preview

Working on Array

Next →

Managing Index File and Web Folders

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App