5 PHP Functions To Sort Arrays

PHP Magic Methods

PHP is popular among almost to all the developers for different reasons. One of them is its vast number of built in function to work with. Today i am going to have some words with some built in PHP sorting functions through which one can easily sort array elements however he/she wants 🙂 . These are the following functions

asort($array) :

This function is used to sort an array passed as the parameter through it with its values.

In the example below an array with the value will be printed with values in ascending order.

$array = array(
 'apple' => 10,
 'nut' => 15,
 'banana' => 5,
 'coconut' => 13
);
asort($array);
var_dump($array);

Output

array (size=4)
  'banana' => int 5
  'apple' => int 10
  'coconut' => int 13
  'nut' => int 15

sort($array):

It is almost identical to asort except that it doesn’t preserve the keys

$array = array(
	'apple' => 10,
	'nut' => 15,
	'banana' => 5,
	'coconut' => 13
);
sort($array);
var_dump($array);

Output:

array (size=4)
  0 => int 5
  1 => int 10
  2 => int 13
  3 => int 15

 

arsort($array):

This also sorts the array with value but in REVERSE ORDER

$array = array(100,2,156,234);
arsort($array);
var_dump($array);

Output:

array (size=4)
  3 => int 234
  2 => int 156
  0 => int 100
  1 => int 2

ksort($array) : 

This function sorts the array with key in ascending order

$array = array(
	'apple' => 10,
	'nut' => 15,
	'banana' => 5,
	'coconut' => 13
);
ksort($array);
var_dump($array);

Output:

array (size=4)
  'apple' => int 10
  'banana' => int 5
  'coconut' => int 13
  'nut' => int 15

Here you can see the the array has been printed where keys has been rearranged alphabetically in ascending order

krsort($array) : 

In this case array keys will be rearranged in descending order

$array = array(
	'apple' => 10,
	'nut' => 15,
	'banana' => 5,
	'coconut' => 13
);
krsort($array);
var_dump($array);

Output:

array (size=4)
  'nut' => int 15
  'coconut' => int 13
  'banana' => int 5
  'apple' => int 10

 

uasort($array,function_name):

It sorts the given array using the function given in the second parameter

 

function user_defined_sort($a,$b)
{
if ($a==$b) return 0;
   return ($a<$b)?-1:1;
}

$array=array('a'=>4,'b'=>2,'c'=>8,'d'=>'6');
uasort($array,'user_defined_sort');
var_dump($array);

Output:

array (size=4)
  'b' => int 2
  'a' => int 4
  'd' => string '6' (length=1)
  'c' => int 8

Here in usort the given array is $array and in second parameter, the user defined function ‘user_defined_sort’ has been provided. So, the argument of this functions are $a and $b. usort function make loop through the elements of $array and each time the current element is argument $a and following element is argument $b of ‘user_defined_sort’ function.

uksort($array):

Almost identical to uasort() except that it sorts the array with the array key  using the user defined function, not with the array value.

function user_defined_sort($a,$b)
{
if ($a==$b) return 0;
   return ($a<$b)?-1:1;
}

$array=array('a'=>4,'b'=>2,'c'=>8,'d'=>'6');
uksort($array,'user_defined_sort');
var_dump($array);

Output:

array (size=4)
  'a' => int 4
  'b' => int 2
  'c' => int 8
  'd' => string '6' (length=1)

Hope , this article will be helpful to understand these functions and their uses. Feel free to  leave a comment and let me know your feelings and share to let others know what you knew.

Thank you !

Leave a Reply

Your email address will not be published. Required fields are marked *

three × 2 =