- 5 years ago
- Zaid Bin Khalid
- 12,014 Views
-
7
In this tutorial, we will figure out how to sort the components or keys of an array with the help of PHP. If want to learn what are arrays then you can click here to learn more about array in PHP.
PHP Functions For Sorting Arrays
There are many functions that you can use to sort arrays in PHP. Some functions use to sort array keys while others can be used to sort arrays values. So, this means in PHP you can sort array in both directions either with keys or values. Below are the sorting arrays functions in PHP.
Sorting Indexed Arrays in Ascending Order
The sort() read every value of the given array and arrange array values from low to high. Consider the below example to understand the usage of sort() function.
<?php
// Define array
$colors = array("Red", "Green", "Blue", "Yellow");
// Sorting and printing array
sort($colors);
print"<pre>";
print_r($colors);
print"</pre>";
?>
The above code result is below.
Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
So also you can use the sort function on the numeric components of the array example is given below.
<?php
// Define array
$numbers = array(1, 2, 3, 4, 7, 10);
// Sorting and printing array
sort($numbers);
print"<pre>";
print_r($numbers);
print"</pre>";
?>
This print_r() proclamation gives the accompanying yield:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
[5] => 10
)
Arranging Indexed Arrays in Descending Order
The rsort() function changes the array order into the descending order of an array. For numeric data, this function will also work and return the same descending ordered data.
<?php
$colors = array("Red2", "Green3", "Blue4", "Yellow1");
rsort($colors);
print"<pre>";
print_r($colors);
print"</pre>";
?>
The above array returns the below data.
Array
(
[0] => Yellow1
[1] => Red2
[2] => Green3
[3] => Blue4
)
Arranging Associative Arrays in Ascending Order By Value
The asort() function changes the array order into the ascending order of an array. For numeric data, this function will also work and return the same ascending ordered data.
<?php
$colors = array("Red2", "Green3", "Blue4", "Yellow1");
rsort($colors);
print"<pre>";
print_r($colors);
print"</pre>";
?>
The above array returns the below data.
Array
(
[2] => Blue4
[1] => Green3
[0] => Red2
[3] => Yellow1
)
Arranging Associative Arrays in Descending Order By Value
The arsort() work sorts the components of an array in descending order by its value.
<?php
$age = array("A"=>20, "B"=>14, "C"=>45, "D"=>35);
arsort($age);
print"<pre>";
print_r($age);
print"</pre>";
?>
The output of the above code is displayed below.
Array
(
[C] => 45
[D] => 35
[A] => 20
[B] => 14
)
Arranging Associative Arrays in Ascending Order By Key
The ksort() work sorts the components of an array in ascending order by its key.
<?php
$age = array("A"=>20, "B"=>14, "C"=>45, "D"=>35);
ksort($age);
print"<pre>";
print_r($age);
print"</pre>";
?>
The result of the above code is given below.
Array
(
[A] => 20
[B] => 14
[C] => 45
[D] => 35
)
Arranging Associative Arrays in Descending Order By Key
The krsort() work sorts the components of an array in descending order by its key.
<?php
$age = array("A"=>20, "B"=>14, "C"=>45, "D"=>35);
krsort($age);
print"<pre>";
print_r($age);
print"</pre>";
?>
The result of the above code is given below.
Array
(
[D] => 35
[C] => 45
[B] => 14
[A] => 20
)
- 5 years ago
- Zaid Bin Khalid
- 12,014 Views
-
7