Today in this blog we will understand the concept of Sorting an Array in PHP. We will be covering the basic functions for arrays sorting in PHP. Array sorting becomes much more important when it comes to complex programming. For example, you want to display the marks of a student in ascending order. Arrays can help you perform these functions with ease. With the help of arrays, you can sort the data in numerical, alphabetical, ascending and descending order as well. Let’s start, first of all, we know about several sort functions for arrays in PHP. There are several sort functions for arrays in PHP like sort(), rsort(), asort(), arsort(), ksort(), krsort(), natsort(), natcasesort(), shuffle(), usort(), uasort(), uksort(). Now, we will understand these sort function in details. sort() Sort an array in ascending order by value. $fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel']; sort($fruits); print_r($fruits); results in Array ( [0] => Apfel [1] => Banane [2] => Orange [3] => Zitrone ) rsort() Sort an array in descending order by value. $fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel']; rsort($fruits); print_r($fruits); results in Array ( [0] => Zitrone [1] => Orange [2] => Banane [3] => Apfel ) asort() Sort an array in ascending order by value and preserve the indices. $fruits = [1 => 'lemon', 2 => 'orange', 3 => 'banana', 4 => 'apple']; asort($fruits); print_r($fruits); results in Array ( [4] => apple [3] => banana [1] => lemon [2] => orange ) arsort() Sort an array in descending order by value and preserve the indices. $fruits = [1 => 'lemon', 2 => 'orange', 3 => 'banana', 4 => 'apple']; arsort($fruits); print_r($fruits); results in Array ( [2] => orange [1] => lemon [3] => banana [4] => apple ) ksort() Sort an array in ascending order by key $fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple']; ksort($fruits); print_r($fruits); results in Array ( [a] => orange [b] => banana [c] => apple [d] => lemon ) krsort() Sort an array in descending order by key. $fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple']; krsort($fruits); print_r($fruits); results in Array ( [d] => lemon [c] => apple [b] => banana [a] => orange ) natsort() Sort an array in a way a human being would do (natural order). $files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack']; natsort($files); print_r($files); results in Array ( [4] => File2.stack [0] => File8.stack [2] => file7.stack [3] => file13.stack [1] => file77.stack ) natcasesort() Sort an array in a way a human being would do (natural order), but case intensive. $files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack']; natcasesort($files); print_r($files); results in Array ( [4] => File2.stack [2] => file7.stack [0] => File8.stack [3] => file13.stack [1] => file77.stack ) shuffle() Shuffles an array (sorted randomly). $array = ['aa', 'bb', 'cc']; shuffle($array); print_r($array); As written in the description it is random so here only one example in what it can result in Array ( [0] => cc [1] => bb [2] => aa ) usort() Sort an array with a user defined comparison function. function compare($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $array = [3, 2, 5, 6, 1]; usort($array, 'compare'); print_r($array); results in Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 ) You May Also Like: Take Image Snapshot from a webcam with Jquery and HTML PHP include() Vs require() Is PHP in 2019 still a relevant language uasort() Sort an array with a user defined comparison function and preserve the keys. function compare($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $array = ['a' => 1, 'b' => -3, 'c' => 5, 'd' => 3, 'e' => -5]; uasort($array, 'compare'); print_r($array); results in Array ( [e] => -5 [b] => -3 [a] => 1 [d] => 3 [c] => 5 ) uksort() Sort an array by keys with a user defined comparison function. function compare($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $array = ['ee' => 1, 'g' => -3, '4' => 5, 'k' => 3, 'oo' => -5]; uksort($array, 'compare'); print_r($array); results in Array ( [ee] => 1 [g] => -3 [k] => 3 [oo] => -5 [4] => 5 ) That’s all for Sorting an Array in PHP. With this, we come to an end to this tutorial and the Basics of PHP array series. I have tried my level best to cover everything basic about Sorting an array in PHP. If you have any doubts or question comment down below. Thanks for Reading, Keep Visiting. Share this:Click to share on Twitter (Opens in new window)Click to share on Facebook (Opens in new window)Click to print (Opens in new window)Click to share on LinkedIn (Opens in new window)Click to share on Telegram (Opens in new window)Click to share on WhatsApp (Opens in new window)Like this:Like Loading... Post navigation Generate Auto Increment Invoice Number With Prefix Using PHP and MySql. Send Email using PHP and AJAX