Reputation: 1527
This array keys is a database primary keys and i need to find biggest value from array and after that update query which need this primary keys. so i need to it will be remain same keys.
My array
Array ( [1] => 7 [2] => 2 [3] => 2 [4] => 10 [5] => 15 [6] => 11 [7] => 40 )
i need this output
Array ( [7] => 40 [5] => 15 [6] => 11 [4] => 10 [1] => 7 )
Upvotes: 2
Views: 50
Reputation: 23880
The PHP arsort function will sort your array in descending order and maintain the indices.
$array = array ( 1 => 7, 2 => 2, 3 => 2, 4 => 10, 5 => 15, 6 => 11, 7 => 40 );
arsort($array);
print_r($array);
Demo: eval.in/836445https://3v4l.org/KQB5Y
To limit it to the top five use array_slice:
print_r(array_slice($array, 0, 5, true));
Upvotes: 2