我有一个数组,其键的格式为[A1] - > [A20],[B1] - > [B20]等,我正在尝试首先使用ksort()
(以正确的顺序获取字母)然后uksort()
对数组进行排序。数组键由'uksort()'函数排序错误
但是,我不知道如何编写uksort()
函数,我的数组键是以奇怪的顺序回来。有人可以请看一看,并告诉我我做错了什么?谢谢。
function _sort_selection_keys($a, $b){
$let_a = substr($a, 0, 1);
$let_b = substr($b, 0, 1);
$num_a = intval(substr($a, 1));
$num_a = intval(substr($b, 1));
/** Check that the first letter is the same. It should be, as the array has already been through 'ksort()', but it's worth checking any way */
if($let_a !== $let_b) :
return strcasecmp($a, $b);
endif;
if($num_a > $num_b) :
return -1;
elseif($num_a = $num_b) :
return 0;
elseif($num_a < $num_b) :
return 1;
endif;
}
为什么不只是:'ksort($ array,SORT_NATURAL);'? – 2013-04-10 16:00:19
完美,谢谢。我不知道你可以这样使用'uksort()',但现在我明白了,这很明显! – 2013-04-10 16:00:46
@ M8R-1jmw5r是的,这也有效。 – xdazz 2013-04-10 16:03:53