开发者

sort $array by $array['value'] Desc

开发者 https://www.devze.com 2023-02-03 09:27 出处:网络
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 associa

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消