Q: Sort array by values while preserving key order I have an array that looks like this:
$array = array(
'5' => 2,
'6' => 3,
'7' => 10,
'8' => 1
);    

and i want it to look like this:
$array = array(
'5' => 1,
'6' => 2,
'7' => 3,
'8' => 10
);

The keys should stay the same but the values should be ordered asscending. With sort() the keys will be deleted and replaced with keys starting at 0. asort() will order the values as i want but it will maintain the index association.
I tried sort, usort, asort but non of these seems to be the orderfunction i need.
Do i have to sort manually or is there a function i can use?
Thanks in advance for any hint.

A: You can extract the keys of your array using array_keys, sort the values using sort and then combine the keys back with the sorted values using array_combine:
$array = array(
'5' => 2,
'6' => 3,
'7' => 10,
'8' => 1
);  
$keys = array_keys($array);
sort($array);
$array = array_combine($keys, $array);
print_r($array);

Output:
Array
(
    [5] => 1
    [6] => 2
    [7] => 3
    [8] => 10
)

Demo on 3v4l.org
