Sorting an associative array in PHP
by THavamani[ Edit ] 2013-03-30 10:44:56
Example Input:
Array ( [a1] => 14959.14 [a2] => 10747.94 [a3] => 19531.23 [a4] => 8915.83 [a5] => 2646.25 [a6] => 28379.74 [a7] => 727.25 )
Syntax:
array_multisort(array1,sorting order,sorting type,array2,array3...)
here,
array1 =Specifies an array(Required)
sorting order = Specifies the sorting order(Optional).
Possible values:
SORT_ASC Default. Sort in ascending order (A-Z)
SORT_DESC sort in descending order (Z-A)
sorting type = Specifies the type to use(Optional),
when comparing elements.
Possible values:
SORT_REGULAR Default. Compare elements normally
SORT_NUMERIC Compare elements as numeric values
SORT_STRING Compare elements as string values
array2,array3 are (optional).
Output Example.
array_multisort($realarea,SORT_DESC);
Descending order Output.
Array ( [a6] => 28379.74 [a3] => 19531.23 [a1] => 14959.14 [a2] => 10747.94 [a4] => 8915.83 [a5] => 2646.25 [a7] => 727.25 )
array_multisort($realarea,SORT_DESC);
Ascending order Output.
Array ( [a7] => 727.25 [a5] => 2646.25 [a4] => 8915.83 [a2] => 10747.94 [a1] => 14959.14 [a3] => 19531.23 [a6] => 28379.74 )