Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Sorting means arranging data in a specific order which can be alphabetical, numerical, increasing or decreasing order according to some linear relationship among data items. It also improves the efficiency of searching. This article focuses on Array Sort In PHP.
Following pointers will be covered in this article,
Using this method, by default the array is sorted in ascending order.
<pre> <?php $var_array = array(10,20,30,40); sort($var_array); print_r($var_array); ?> </pre>
Output:
Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)
Moving further let us take a look at this,
Array is sorted in descending order.
<pre> <?php $alphabets = array('a','h','c','f'); rsort($alphabets); foreach ($alphabets as $key => $val) { echo "$key = $valn"; } ?> </pre>
Output:
0 = h
1 = f
2 = c
3 = a
Third method in this topic is arsort
Associative arrays are sorted in descending order, according to value.
<pre> <?php $friends = array("a" => "Tarun", "q" => "ashok", "b" => "charan", "l" => "sabid"); arsort($friends); foreach ($friends as $key => $val) { echo "$key = $valn"; } ?> </pre>
Output:
l = sabid
b = charan
q = ashok
a = Tarun
Let us try and understand how krsort works,
Associative arrays are sorted in descending order, according to the key.
<pre> <?php $var_array= array("1"=>"Ashok", "2"=>"Tarun", "3"=>"charan","4"=>"sabid","5"=>"adarsh","6"=>"chintan","7"=>"vaibhav"); krsort($var_array); print_r($var_array); ?> </pre>
Output:
Array
(
[7] => vaibhav
[6] => chintan
[5] => adarsh
[4] => sabid
[3] => charan
[2] => Tarun
[1] => Ashok
)
Let us move on to the next topic of this article,
Associative arrays are sorted in ascending order, according to value.
<pre> <?php $var_array= array("1"=>"Ashok", "2"=>"Tarun", "3"=>"charan","4"=>"sabid","5"=>"adarsh","6"=>"chintan","7"=>"vaibhav"); asort($var_array); print_r($var_array); ?> </pre>
Output:
Array
(
[1] => Ashok
[2] => Tarun
[5] => adarsh
[3] => charan
[6] => chintan
[4] => sabid
[7] => vaibhav
)
It is time to move to the next topic in this article,
Associative arrays are sorted in ascending order, according to key
<pre> <?php $var_array= array("7"=>"vaibhav","6"=>"chintan","1"=>"Ashok","5"=>"adarsh", "2"=>"Tarun", "3"=>"charan","4"=>"sabid"); ksort($var_array); print_r($var_array); ?> </pre>
Output:
Array
(
[1] => Ashok
[2] => Tarun
[3] => charan
[4] => sabid
[5] => adarsh
[6] => chintan
[7] => vaibhav
)
Array is sorted by using a “natural order” algorithm. It sorts in such a way that orders alphanumeric strings in the way a human being would maintain key or value associations.
<pre> <?php $var_files=array("file1.php","file2.php", "file3.php", "file0.php"); natsort($var_files); print_r($var_files); ?> </pre>
Output:
Array
(
[3] => file0.php
[0] => file1.php
[1] => file2.php
[2] => file3.php
)
Array is sorted using a case insensitive “natural order” algorithm.
<pre> <?php $var_files = array("file12.php", "File22.txt","file2.php", "file3.php", "File1.php"); natcasesort($var_files); print_r($var_files); ?> </pre>
Output:
Array
(
[4] => File1.php
[2] => file2.php
[3] => file3.php
[0] => file12.php
[1] => File22.txt
)
Next we are going to take a look at uasort
Array is sorted using a user-defined comparison function and maintain index association.
<pre> <?php function fun($a, $b) { if ($a== $b) return 0; return ($a > $b) ? -1 : 1; } $array = array('a' => -1, 'b' => 6, 'c' => 8, 'd' => -9, 'e' => 1, 'f' => 5, 'g' => 3); uasort($array, 'fun'); print_r($array); ?> </pre>
Output:
Array
(
=> 8
[b] => 6
[f] => 5
[g] => 3
[e] => 1
[a] => -1
[d] => -9
)
This brings us to the final bit of this Array Sort In PHP article
Array is sorted by keys using a user-defined comparison function
<pre> <?php function fun($a, $b) { if ($a== $b) return 0; return ($a > $b) ? -1 : 1; } $array = array('a' => -1, 'b' => 6, 'c' => 8, 'd' => -9, 'e' => 1, 'f' => 5, 'g' => 3); uksort($array, 'fun'); print_r($array); ?> </pre>
Output:
Array
(
[g] => 3
[f] => 5
[e] => 1
[d] => -9
=> 8
[b] => 6
[a] => -1
)
usort():Array Sort In PHP
Array is sorted by values using a user-defined comparison function.
<pre> <?php function fun($a, $b) { if ($a== $b) return 0; return ($a > $b) ? -1 : 1; } $array = array('a' => -1, 'b' => 6, 'c' => 8, 'd' => -9, 'e' => 1, 'f' => 5, 'g' => 3); usort($array, 'fun'); print_r($array); ?> </pre>
Output:
Array
(
[0] => 8
[1] => 6
[2] => 5
[3] => 3
[4] => 1
[5] => -1
[6] => -9
)
With this we come to an end of this article, I hope you have learned about the all the array sort functions used in PHP. If you found this article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Discover how to create mobile apps that look and feel great on any platform with a comprehensive Flutter Course.
Got a question for us? Please mention it in the comments section of this article and I will get back to you.
edureka.co