How to sort associative array with negative values in descending order with PHP

1 Answer

0 votes
$array = array("php"=>-3, "c"=>-12, "c++"=>-7, "python"=>-18, "java"=>-11);
 
asort($array, SORT_NUMERIC);

$array = array_reverse($array, true);

foreach ($array as $key => $value) {
    echo $key . " " . $value . "\n";
}
  
  
  
/*
run:
  
php -3
c++ -7
java -11
c -12
python -18
  
*/

 



answered Sep 23, 2023 by avibootz

Related questions

2 answers 207 views
1 answer 160 views
1 answer 183 views
1 answer 182 views
1 answer 182 views
2 answers 173 views
1 answer 110 views
...