-2

I have following associative array:

$weight["fan"]=10;
$weight["clock"]=2;
$weight["wheel"]=12;
$weight["Bicycle"]=20;

How should I sort the above array ascending or descending w.r.t value as follows:

$weight["Bicycle"]=20;
$weight["wheel"]=12;
$weight["fan"]=10;
$weight["clock"]=2;

Regards,

  • possible duplicate of [How to sort an array of associative arrays by value of a given key in PHP](http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php) – Lix Jul 02 '14 at 11:51
  • Possible duplicate of http://stackoverflow.com/search?q=%5Bphp%5D+sort+array+by+values – PeeHaa Jul 02 '14 at 11:52

2 Answers2

0

Using arsort you can sort like you want to.

arsort($weight);
idmean
  • 14,540
  • 9
  • 54
  • 83
0

Simply use -

arsort($weight);

This sort the array in reverse order, and keep the keys intact.

Anik
  • 441
  • 4
  • 7