简体   繁体   English

按$ array ['value'] Desc排序$ array

[英]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 ? 例如,我有一个$array ,其中我使用$array['name']$array['value']我可以按属性之一对$array进行排序,然后选择ASC / DESC?

To sort an associative array according to the key of the array, you can use the ksort() function in the following manner: 要根据数组的键对关联数组进行排序,可以按以下方式使用kso​​rt()函数:

<?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. 同样,您可以使用krsort()函数根据键以升序对关联数组进行排序。

Source: http://www.webcheatsheet.com/PHP/sorting_array.php 来源: http//www.webcheatsheet.com/PHP/sorting_array.php

You can use usort : 您可以使用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. asort排序值, arsort反向。

ksort sorts keys, krsort in reverse. ksort排序键, krsort相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM