PHP Arrays Sorting
This array elements use an array type data can be sorted in alphabetical or numerical order, descending or ascending.
PHP - Sorting Function For Array
Function | Description |
---|---|
sort() | use this function we can sort the array in ascending order |
rsort() | use this function we can sort the array in descending order |
asort() | use this function we can sort associative arrays in ascending order, according to the value |
ksort() | use this function we can sort associative arrays in ascending order, according to the key |
arsort() | use this function we can sort associative arrays in descending order, according to the value |
krsort() | use this function we can sort associative arrays in descending order, according to the key |
Sort Array in Ascending Order -PHP sort()
The following example sorts the elements of the $mobiles array in ascending alphabetical and numerical order:
Syntax
//elements list
sort(elements);
Example
<?php
$mobiles = array("Nokia", "Oppo", "iphone","Samsung");
sort($mobiles);
?>
<?php
$numbers = array(4, 6, 1, 26, 15);
sort($numbers);
?>
Out put
Nokia
Oppo
Samsung
iphone
1
4
6
15
26
Sort Array in Descending Order -PHP rsort()
The following example sorts the elements of the $numberlist array in descending alphabetical and numerical order:
Syntax
//elements list
rsort(elements);
Example
<?php
$mobiles = array("Nokia", "Oppo", "iphone","Samsung");
rsort($mobiles);
?>
<?php
$numbers = array(4, 6, 1, 26, 15);
rsort($numbers);
?>
Out put
Nokia
Oppo
iphone
Samsung
26
15
6
4
1
Sort Array Ascending Order, According to Value -PHP asort()
The following example sorts the associative array in ascending order, according to the array value:
Syntax
//elements list
asort(elements);
Example
<?php
$mobiles = array("nokia"=>100020, "oppo"=>1500, "iphone"=>5000,"samsung"=>10000);
asort($mobiles);
print_r($mobiles);
?>
Out put
Array ( [oppo] => 1500 [iphone] => 5000 [samsung] => 10000 [nokia] => 100020 )
Sort Array Ascending Order, According to Key -PHP ksort()
The following example sorts the associative array in ascending order, according to the array Key:
Syntax
//elements list
ksort(elements);
Example
<?php
$mobiles = array("nokia"=>100020, "oppo"=>1500, "iphone"=>5000,"samsung"=>10000);
ksort($mobiles);
print_r($mobiles);
?>
Out put
Array ( [iphone] => 5000 [nokia] => 100020 [oppo] => 1500 [samsung] => 10000 )
Sort Array Descending Order, According to Value -PHP arsort()
The following example sorts the associative array in descending order, according to the array Value:
Syntax
//elements list
arsort(elements);
Example
<?php
$mobiles = array("nokia"=>100020, "oppo"=>1500, "iphone"=>5000,"samsung"=>10000);
arsort($mobiles);
print_r($mobiles);
?>
Out put
Array ( [nokia] => 100020 [samsung] => 10000 [iphone] => 5000 [oppo] => 1500 )
Sort Array Descending Order, According to Key-PHP krsort()
The following example sorts the associative array in descending order, according to the array Key:
Syntax
//elements list
krsort(elements);
Example
<?php
$mobiles = array("nokia"=>100020, "oppo"=>1500, "iphone"=>5000,"samsung"=>10000);
krsort($mobiles);
print_r($mobiles);
?>
Out put
Array ( [samsung] => 10000 [oppo] => 1500 [nokia] => 100020 [iphone] => 5000 )
Complete reference of all array functions, go to our complete PHP Array Function. Click Here ?