Rumah >pembangunan bahagian belakang >tutorial php >请教一个PHP排序的问题
我要对数组进行数值排序,代码如下:
<code>$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10); asort($a, SORT_NUMERIC); var_dump($a); </code>
<code>array(5) { 'd' => int(10) 'a' => int(10) 'c' => int(10) 'z' => int(10) 'b' => int(11) } </code>
举例:
上面的数组
<code>$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10); </code>
根据上面排序后,键的顺序依次为
d => 10, a => 10, c => 10, z => 10, b => 11
我希望能实现的顺序为为
a => 10, z => 10, c => 10, d => 10, b => 11
我要对数组进行数值排序,代码如下:
<code>$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10); asort($a, SORT_NUMERIC); var_dump($a); </code>
<code>array(5) { 'd' => int(10) 'a' => int(10) 'c' => int(10) 'z' => int(10) 'b' => int(11) } </code>
举例:
上面的数组
<code>$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10); </code>
根据上面排序后,键的顺序依次为
d => 10, a => 10, c => 10, z => 10, b => 11
我希望能实现的顺序为为
a => 10, z => 10, c => 10, d => 10, b => 11
http://3v4l.org/fsQkS
<code>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 ) **/ </code>