I want to sort the arrays numerically, with the following code:
$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);asort($a, SORT_NUMERIC);var_dump($a);
The printing results are as follows:
array(5) { 'd' => int(10) 'a' => int(10) 'c' => int(10) 'z' => int(10) 'b' => int(11)}
Problem:
- I need to sort the values (implemented)
- If the values are the same, the order position of the array keys does not change, how do I do
Example:
The above array
$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);
After sorting, the order of the keys is
D = ten, a = ten, c = ten, z = ten, B = 11
I want to be able to implement the order for
A = ten, z = ten, c = ten, d = ten, B = 11
Reply content:
I want to sort the arrays numerically, with the following code:
$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);asort($a, SORT_NUMERIC);var_dump($a);
The printing results are as follows:
array(5) { 'd' => int(10) 'a' => int(10) 'c' => int(10) 'z' => int(10) 'b' => int(11)}
Problem:
- I need to sort the values (implemented)
- If the values are the same, the order position of the array keys does not change, how do I do
Example:
The above array
$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);
After sorting, the order of the keys is
D = ten, a = ten, c = ten, z = ten, B = 11
I want to be able to implement the order for
A = ten, z = ten, c = ten, d = ten, B = 11
Http://3v4l.org/fsQkS
function hasort($arr) { $a = $b = array(); foreach($arr as $v => $k) $a[$k][] = $v; ksort($a); foreach($a as $v => $i) foreach($i as $k) $b[$k] = $v; return $b;}$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);$a = hasort($a);print_r($a);/**Array( [a] => 10 [z] => 10 [c] => 10 [d] => 10 [b] => 11)**/