-4

As the title says. For example:

Original

{18=>13, 0=>13, 27=>13, 9=>13, 19=>13, 12=>12, 21=>12, 31=>12, 4=>12, 22=>12}

I want it become like this:

{0=>13, 9=>13, 18=>13, 19=>13, 27=>13, 4=>12, 12=>12, 21=>12, 22=>12, 31=>12}

Can anyone help? Thanks.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
benleung
  • 871
  • 4
  • 12
  • 25
  • 2
    Did you even search SO?... http://stackoverflow.com/questions/874181/php-sort-an-array?rq=1 OR http://stackoverflow.com/questions/2282013/php-array-multiple-sort-by-value-then-by-key?rq=1 – Daryl Gill Jan 25 '13 at 15:45
  • I want the value in descending order and the key in ascending order.And I tried to second link, It doesn't work.@DarylGill – benleung Jan 25 '13 at 15:55

3 Answers3

0

http://php.net/manual/en/array.sorting.php

$array = //Your array value

asort($array);
ksort($array);
Grambot
  • 4,370
  • 5
  • 28
  • 43
0

Give it a try

$array = array(array_values($array),array_keys($array));
array_multisort($array[0], SORT_DESC, $array[1], SORT_ASC);
$sorted_array = array_combine($array[1],$array[0]);
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
-2

Take a look at ksort

http://php.net/manual/en/function.ksort.php

For example...

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
  echo "$key = $val\n";
}
?>

Output

a = orange

b = banana

c = apple

d = lemon

philipbrown
  • 554
  • 3
  • 8