sort by keep keys and keep value order?

Issue

I have an

$a = array(9=>"a",8=>"c",5=>"d");

I want to sort the only keys of array $a and keep order of values.

so it will be array(5=>"a",8=>"c",9=>"d");

How Could I DO in php array?

Solution

Sorting the keys, but keep the values in order is not possible by just ordering, because it would result in a new array. This is also the solution: Create a new array

$keys = array_keys($a);
sort($keys);
$result = array_combine($keys, array_values($a));

Answered By – KingCrunch

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published