Home Php PHP Sorting Arrays Tutorial
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

PHP Sorting Arrays Tutorial

Advertisement
Learn how to sort your arrays in different ways with this tutorial.

PHP Sorting Arrays

     

Sorting an array is the most useful thing you can do with an array. We will learn how to sort an array using key as well as value.

Sort By Value:

PHP provides many functions to sort an array. We can sort an array by value, key or randomly. But in most of the situations we need to sort an array by value. There are so many functions are available in PHP, some of these are discussed below:

i)  sort()

ii)  asort()

iii)  rsort()

iv)  arsort()

v)  natsort()

vi)  natcasesort()

 

i)  sort(): To sort an array we need to use sort() function. Small description of sort is given below:

General Format bool sort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameters array: the input array sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

Example 1:

<?php

$car=array("manza","safari","nano","vista","jaguar","land rover");

echo "<b>Before sorting the values are:</b>";

print_r($car);

sort($car);

echo"<br/>";

echo "<b>After sorting values become:</b>";

print_r($car);

?>

Output:

Before sorting the values are: Array ( [0] => manza [1] => safari [2] => nano [3] => vista [4] => jaguar [5] => land rover )
After sorting values become: Array ( [0] => jaguar [1] => land rover [2] => manza [3] => nano [4] => safari [5] => vista )

 

ii)  asort(): If we examine the above code and the result we can see that after being sorted the elements of the array are arranged in alphabetical order. Another method to sort the array is asort(). This function sorts an array and also maintain the index position. Small description of asort() is:

General Format bool asort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameter array: the input array

 

sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

Example 2:

<?php

$car=array("m"=>"manza","s"=>"safari","n"=>"nano","v"=>"vista","j"=>"jaguar","l"=>"land rover");

echo "<b>Before sorting the values are:</b>";

print_r($car);

asort($car);

echo"<br/>";

echo "<b>After sorting values become:</b>";

print_r($car);

?>

Output:

Before sorting the values are:Array ( [m] => manza [s] => safari [n] => nano [v] => vista [j] => jaguar [l] => land rover )
After sorting values become:Array ( [j] => jaguar [l] => land rover [m] => manza [n] => nano [s] => safari [v] => vista )

iii)  rsort(): There are several functions are available in PHP, rsort() is one of them, it sorts an array in reverse order, rsort() is just opposite sort() function:

General Format bool rsort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameter array: the input array

 

sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

 

Example 3:

<?php

$car=array("manza","safari","nano","vista","jaguar","land rover");

echo "<b>Before sorting the values are:</b>";

print_r($car);

rsort($car);

echo"<br/>";

echo "<b>After sorting values become:</b>";

print_r($car);

?>

Output:

Before sorting the values are:Array ( [0] => manza [1] => safari [2] => nano [3] => vista [4] => jaguar [5] => land rover )
After sorting values become:Array ( [0] => vista [1] => safari [2] => nano [3] => manza [4] => land rover [5] => jaguar )

iv)  arsort():  If we consider the name of the function then it will be clear that this function will sort an array in reverse order and maintain index association like asort() + rsort() + sort()=arsort(). Small description of the function is as follows:

 

General Format bool arsort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameter array: the input array

 

sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

Example 4:

<?php

$car=array("m"=>"manza","s"=>"safari","n"=>"nano","v"=>"vista","j"=>"jaguar");

echo "<b>Before sorting the values are:</b>";

print_r($car);

arsort($car);

echo"<br/>";

echo "<b>After sorting values become:</b>";

print_r($car);

?>

Output:

Before sorting the values are:Array ( [m] => manza [s] => safari [n] => nano [v] => vista [j] => jaguar )
After sorting values become:Array ( [v] => vista [s] => safari [n] => nano [m] => manza [j] => jaguar )

v)  natsort(): This function of PHP sorts an array using "natural order" algorithm, meaning of "natural order" algorithm is it sorts alphanumeric strings in such a way that human sorts the key/value associations. Description is as follows:

 

General Format bool natsort(array $array)
Return Value True/False
Parameter array: the input array

 

Example 5:

<?php

$array1=$array2=array("Emp01","Emp10","Emp23","Emp04");

echo"<b> Before Simple sorting the values are:</b><br/>";

print_r($array1);

sort($array1);

echo"<br/><b> After Simple sorting the values are:</b><br/>";

print_r($array1);

natsort($array2);

echo"<br/><b> After natural sorting the values are:</b><br/>";

print_r($array2);

?>

Output:

Before Simple sorting the values are:
Array ( [0] => Emp01 [1] => Emp10 [2] => Emp23 [3] => Emp04 )
After Simple sorting the values are:
Array ( [0] => Emp01 [1] => Emp04 [2] => Emp10 [3] => Emp23 )
After natural sorting the values are:
Array ( [0] => Emp01 [3] => Emp04 [1] => Emp10 [2] => Emp23 )

 

vi)  natcasesort():   Sort an array using a case insensitive "natural algorithm" manner. To know more about natural algorithm please read natsort() which is given above. Description of natcasesort() is given below:

General Format bool natcasesort(array $array)
Return Value True/False
Parameter array: the input array

 

Example 6:

<?php

$array1=$array2=$array3=array("emp01","Emp10","emp23","Emp04");

echo"<b> Before Simple sorting the values are:</b><br/>";

print_r($array1);

echo"<br/><b> After Simple sorting the values are:</b><br/>";

sort($array1);

print_r($array1);

echo"<br/><b> After natural sorting the values are:</b><br/>";

natsort($array2);

print_r($array2);

echo"<br/><b> After natural case insensitive sorting the values are:</b><br/>";

natcasesort($array3);

print_r($array3);

?>

Output:

Before Simple sorting the values are:
Array ( [0] => emp01 [1] => Emp10 [2] => emp23 [3] => Emp04 )
After Simple sorting the values are:
Array ( [0] => Emp04 [1] => Emp10 [2] => emp01 [3] => emp23 )
After natural sorting the values are:
Array ( [3] => Emp04 [1] => Emp10 [0] => emp01 [2] => emp23 )
After natural case insensitive sorting the values are:
Array ( [0] => emp01 [3] => Emp04 [1] => Emp10 [2] => emp23 )

 

Sort Array by Key

In PHP there are are so many built in functions are available which make this language easier. In general we sort the values of an array, but sometimes we need to sort the keys instead of values, PHP provided us three functions to sort any array by  key. These are:

i)  ksort()

ii)  krsort()

Now we will discuss these functions one by one in detail:

i)  ksort(): This function sorts an array by key maintaining the correlation with the value, this function is useful for associative array.

General Format bool ksort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameters array: the input array sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

 

Example 1:

<?php

$array=array(30=>"twitter",11=>"orkut",7=>"facebook");

echo"<b>Before sorting the keys of the array is:</b><br/>";

print_r($array);

ksort($array);

echo"<br/><b>Before sorting the keys of the array is:</b><br/>";

print_r($array);

?>

Output:

Before sorting the keys of the array is:
Array ( [30] => twitter [11] => orkut [7] => facebook )
After sorting the keys of the array is:
Array ( [7] => facebook [11] => orkut [30] => twitter )

From the above example we can see that the sorting is done on the key instead of value.

ii)  krsort(): This function sorts an array by key in reverse order maintaining the correlation with the value, this function is useful for associative array.

General Format bool krsort(array $array [,int $sort_flags=SORT_REGULAR] )
Return Value True/False
Parameters array: the input array sort_flags:
  • sort_regular: normal comparison
  • sort_numeric: numeric comparison
  • sort_string: string comparison

 

Example 2:

<?php

$array=array(30=>"twitter",11=>"orkut",7=>"facebook");

echo"<b>Before sorting the keys of the array is:</b><br/>";

print_r($array);

krsort($array);

echo"<br/><b>After sorting the keys in reverse order:</b><br/>";

print_r($array);

?>

Output:

Before sorting the keys of the array is:
Array ( [30] => twitter [11] => orkut [7] => facebook )
After sorting the keys in reverse order:
Array ( [30] => twitter [11] => orkut [7] => facebook )

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 12, 2009

Related Tutorials

Ask Questions?    Discuss: PHP Sorting Arrays Tutorial  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
mila
May 18, 2014
hi

good
DMCA.com