Syntax: sort(array &$array, int $flags = SORT_REGULAR): bool. The flags parameter modifies the sorting behavior. The function returns true. Basic sort ExampleThis demonstrates sorting a simple numeric array in
In this chapter, we will go through the following PHP array sort functions: 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,...
PHP Version:4+ PHP Changelog:PHP 8.2.0: Now returns TRUE (previously it returned bool) More Examples Example Sort the elements of the $numbers array in ascending numerical order: <?php $numbers =array(4,6,2,22,11); sort($numbers); ...
The sort() function is used to sort an indexed array in ascending order. If array elements are numeric, it sorts based on the numbers, if array elements are the string, it sorts based on the alphabets and if the array contains numeric values and text/strings, it sorts elements based on ...
1. Sort Array of Strings in Ascending Order In the following example, we will take an array of strings, and sort the array in ascending order lexicographically usingsort()function. PHP Program </> Copy <?php $names = array("banana", "cherry", "apple", "mango"); ...
PHP 5.3: Added sorting type SORT_LOCALE_STRING More Examples Example Return a sorted array in ascending order: <?php $a1=array("Dog","Cat"); $a2=array("Fido","Missy"); array_multisort($a1,$a2); print_r($a1); print_r($a2); ...
The sorting type SORT_LOCALE_STRING was added in PHP 5.3. More Examples Example 1 Return a sorted array in ascending order: <?php $a1=array("Dog","Cat"); $a2=array("Fido","Missy"); array_multisort($a1,$a2); print_r($a1); ...
Some more functions are there in PHP to sort an array based on keys. These are,ksort() – It is as like as sort() which will sort array elements in ascending order, but, based on key instead. krsort() – This function is used to sort an array based on its key but in reverse ...
Example - Array Ascending Order $array = array( array('price'=>'1000.50','product'=>'product 1'), array('price'=>'8800.50','product'=>'product 2'), array('price'=>'200.0','product'=>'product 3') ); function cmp($a, $b) { ...
// Swift program to sort an integer array// in ascending orderimport Swift var arr:[Int]=[12,10,25,20,50] print("Array before sorting: ",arr) arr.sort() print("Array after sorting: ",arr) Output: Array before sorting: [12, 10, 25, 20, 50] Array after sorting: [10, 12, 20...