简体   繁体   中英

sort an array by key and by value too in PHP

How to sort an array by ascending key and descending value? 在此处输入图片说明

    array_multisort(array_values($array), SORT_DESC, array_keys($array), SORT_ASC, $array);

Here is the code you are looking for.

<?php
$array = array(5=>100,4=>100,3=>100,1=>100,8=>97,6=>97,9=>82,7=>80);

$keys = array_keys($array);
$values = array_values($array);

sort($keys);
rsort($values);
for($i=0;$i<=count($keys)-1;$i++){
    $array[$keys[$i]] = $values[$i];
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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