sort $array by $array['value'] Desc
How can i do that开发者_如何转开发? example i have an $array
where i use $array['name']
and $array['value']
i can i sort the $array
by one of the atributes and choose ASC / DESC ?
To sort an associative array according to the key of the array, you can use the ksort() function in the following manner:
<?php
$narray["IBM"]="International Business Machines";
$narray["MS"]="Microsoft";
$narray["CA"]="Computer Associated";
$narray["WHO"]="World Health Organization";
$narray["UK"]="United Kingdon";
$narray["BA"]="Something Random";
ksort($narray);
foreach($narray as $key => $value)
{
print $key . " = " . $value . "<br />";
}
?>
Similarly, you can sort an associative array according to the key, in ascending order by using the krsort() function.
Source: http://www.webcheatsheet.com/PHP/sorting_array.php
You can use usort
:
function my_array_sorter($a, $b)
{
return strcmp($a['name'], $b['name']);
}
usort($my_array, 'my_array_sorter');
If you mean you are using associative arrays:
arsort($array)
asort
sorts values, arsort
in reverse.
ksort
sorts keys, krsort
in reverse.
精彩评论