I need to sort an array that has this form :
Array
(
[18] => 9
[14] => 4
[53] => 9
[10749] => 4
[28] => 9
[12] => 6
[878] => 7
[35] => 8
[10769] => 1
[9648] => 1
[10751] => 1
[27] => 1
[80] => 3
)
The arsort function gives me :
Array
(
[53] => 9
[28] => 9
[18] => 9
[35] => 8
[878] => 7
[12] => 6
[14] => 4
[10749] => 4
[80] => 3
[27] => 1
[10769] => 1
[9648] => 1
[10751] => 1
)
That’s good but the thing is that when values are the same I would like to get them sorted by their keys, is that possible ? so i would get :
Array
(
[18] => 9
[28] => 9
[53] => 9
[35] => 8
[878] => 7
[12] => 6
[14] => 4
[10749] => 4
[80] => 3
[27] => 1
[9648] => 1
[10751] => 1
[10769] => 1
)
Can anybody tell me how to do this?
here’s a solution using php functions instead of the very slow bubblesort
$arr = array( '18' => 9, '14' => 4, '53' => 9, '10749' => 4, '28' => 9, '12' => 6, '878' => 7, '35' => 8, '10769' => 1, '9648' => 1, '10751' => 1, '27' => 1, '80' => 3 ); print_r($arr); arsort( $arr ); print_r($arr); $last = null; $tmp = array(); $result = array(); foreach( $arr as $k => $v ) { if ( $last && $v != $last ) { krsort($tmp); foreach ( $tmp as $kt => $vt ) { $result[ $kt ] = $vt; } $tmp = array(); } $tmp[$k] = $v; $last = $v; } krsort($tmp); foreach ( $tmp as $kt => $vt ) { $result[ $kt ] = $vt; } print_r($result);