What does sort () do in php?
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending. Show
PHP - Sort Functions For ArraysIn this chapter, we will go through the following PHP array sort functions:
Sort Array in Ascending Order - sort()The following example sorts the elements of the $cars array in ascending alphabetical order: Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » The following example sorts the elements of the $numbers array in ascending numerical order: Sort Array in Descending Order - rsort()The following example sorts the elements of the $cars array in descending alphabetical order: Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » The following example sorts the elements of the $numbers array in descending numerical order: Sort Array (Ascending Order), According to Value - asort()The following example sorts an associative array in ascending order, according to the value: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Ascending Order), According to Key - ksort()The following example sorts an associative array in ascending order, according to the key: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Descending Order), According to Value - arsort()The following example sorts an associative array in descending order, according to the value: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Descending Order), According to Key - krsort()The following example sorts an associative array in descending order, according to the key: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Complete PHP Array ReferenceFor a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! PHP Exercises(PHP 4, PHP 5, PHP 7, PHP 8) sort — Sort an array in ascending order Descriptionsort(array
Parametersarray The input array. flags The optional second parameter Sorting type flags:
Return Values Always returns ExamplesExample #1 sort() example
= array("lemon", "orange", "banana", "apple"); The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange The fruits have been sorted in alphabetical order. Example #2 sort() example using case-insensitive natural ordering
= array( The above example will output: fruits[0] = Orange1 fruits[1] = orange2 fruits[2] = Orange3 fruits[3] = orange20 The fruits have been sorted like natcasesort(). Notes
Warning Be careful when sorting arrays with mixed types values because sort() can produce unexpected results, if See Also
phpdotnet at m4tt dot co dot uk ¶ 12 years ago
count($array) > 0) { aminkhoshzahmat at gmail dot com ¶ 2 years ago
= array('Amin', 'amir', 'sarah', 'Somayeh', 'armita', 'Armin');sort($names); // simple alphabetical sort ajanata at gmail dot com ¶ 10 years ago
joris at mangrove dot nl ¶ 15 years ago
Walter Tross ¶ 10 years ago
danm68 at gmail dot com ¶ 13 years ago
peek at mailandnews dot com ¶ 21 years ago
eriewave at hotmail dot com ¶ 12 years ago
($array, ksort($array))?>
williamprogphp at[pleaseNOTSPAM] yahoo d ¶ 8 years ago
alishahnovin at hotmail dot com ¶ 15 years ago
?>
g8z at yahoo dot com ¶ 16 years ago
define( 'ASC_AZ', 1000 );
petr dot biza at gmail dot com ¶ 13 years ago
$temp_array as $key => &$value) {
$sort = array();
matpatnik at hotmail dot com ¶ 14 years ago
Md. Abutaleb ¶ 2 years ago
$a = ['fruit'=> 'apple', 'A' => 10, 20, 5, 2.5, 5=>'A new value', 'last' => 'value', TRUE, NULL, "", FALSE, array(), new StdClass];
r at rcse dot de ¶ 3 years ago
Abhishek Banerjee ¶ 6 years ago
count($array) > 0) { aditycse at gmail dot com ¶ 7 years ago
me[ at ]szczepan[ dot ]info ¶ 9 years ago
james at miicro dot net ¶ 17 years ago
alex dot hristov dot 88 at gmail dot com ¶ 11 years ago
stepmuel at ee dot ethz dot ch ¶ 13 years ago
getIndex($obj) { alex [at] vkpb [dot] com ¶ 15 years ago
g8z at yahoo dot com ¶ 16 years ago
define( 'ASC_AZ', 1000 ); Emiliyan at ServicesBG dot Com ¶ 16 years ago
nm at thenoodleman dot com ¶ 16 years ago
timc at hlyw dot com ¶ 17 years ago
cmarshall at gmx dot de ¶ 11 years ago
function compare_towns($a, $b) poulou_0 at hotmail dot com ¶ 11 years ago
Brecht Cloetens ¶ 12 years ago
raul at jimi dot com dot mx ¶ 16 years ago
sinan at sinaneldem dot com ¶ 15 years ago
= array('apple', 'banana','pear'); anaz114119 at gmail dot com ¶ 11 years ago
anthony at ectrolinux dot com ¶ 18 years ago
www at designdetector dot com ¶ 14 years ago
' jesper at snt dot utwente dot nl ¶ 16 years ago
foo($i, $v) What is the use of sort () function in PHP?The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. It sorts the actual array and hence changes are reflected in the original array itself. The function provides us with 6 sorting types, according to which the array can be sorted.
What is the use of sort function?The rsort() is an inbuilt function in PHP and is used to sort the array in descending order i.e, greatest to smallest. It sorts the actual array and hence changes are reflected in the array itself.
How do you sort in PHP?PHP Sorting Arrays. sort() - sort arrays in ascending order.. rsort() - sort arrays in descending order.. asort() - sort associative arrays in ascending order, according to the value.. ksort() - sort associative arrays in ascending order, according to the key.. What algorithm does PHP sort use?For sorting, PHP uses an implementation of quicksort that can be found in Zend/zend_sort. c , which takes a comparison function and an array of elements. The default comparison function for sort() is defined in ext/standard/array.
|