PHP Value Sorting


 

PHP Value Sorting

PHP Value Sorting: This tutorial has covered many functions which are used to sort the values of an array, many examples are included to show how one function is different from another.

PHP Value Sorting: This tutorial has covered many functions which are used to sort the values of an array, many examples are included to show how one function is different from another.

PHP 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 in PHP. 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);

0

echo"<br/>";

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

print_r($car);

1

?>

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 )

2

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:

3

<?php

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

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

4

print_r($car);

rsort($car);

echo"<br/>";

5

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

print_r($car);

?>

6

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:

7

 

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

8

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

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

print_r($car);

9

arsort($car);

echo"<br/>";

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

0

print_r($car);

?>

Output:

1

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:

 

2
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");

3

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

print_r($array1);

sort($array1);

4

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

print_r($array1);

natsort($array2);

5

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

print_r($array2);

?>

6

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 )

 

7

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

8

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

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

print_r($array1);

9

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

sort($array1);

print_r($array1);

0

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

natsort($array2);

print_r($array2);

1

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

natcasesort($array3);

print_r($array3);

2

?>

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 )

3

Ads