How to sort associative arrays by values in descending order PHP

2 Answers

0 votes
$array = array("php"=>"99", "c"=>"108", "c++"=>"31", "python"=>"21");

arsort($array);
 
foreach ($array as $key => $value) {
    echo $key . " " . $value . "\n";
}
  
  
  
/*
run:
  
c 108
php 99
c++ 31
python 21
  
*/

 



answered Mar 5, 2021 by avibootz
edited Sep 23, 2023 by avibootz
0 votes
$array = array("php"=>3, "c"=>12, "c++"=>7, "python"=>18, "java"=>11);
 
arsort($array);
 
foreach ($array as $key => $value) {
    echo $key . " " . $value . "\n";
}
  
  
  
/*
run:
  
python 18
c 12
java 11
c++ 7
php 3
  
*/

 



answered Sep 23, 2023 by avibootz
...