我有一个像这样的数组:
$a = array("MA1" => 0, "MA10" => 1, "MA20" => 2, "MA5" => 3, "SM10" => 4, "SM8" => 5, "SM20" => 6, "SN33" => 7);
我想对它进行排序,我将按以下顺序排序:
$a = array("MA1" => 0, "MA5" => 3, "MA10" => 1, "MA20" => 2, "SM8" => 5, "SM10" => 4, "SM20" => 6, "SN33" => 7);
所以我需要一个在前两个字母中按字母顺序排列的订单和其余字母的数字。所以我认为我必须用
做到这一点uksort($a, "cmp");
所以我需要一个这样的函数:
function cmp($a, $b) {
// ???
return strcasecmp($a, $b);
}
我如何编写函数以使订单正确?
提前谢谢你&最诚挚的问候。
答案 0 :(得分:3)
您可以使用内置的自然比较功能:
$a = array("MA1" => 0, "MA10" => 1, "MA20" => 2, "MA5" => 3, "SM10" => 4, "SM8" => 5, "SM20" => 6, "SN33" => 7);
uksort($a, "strnatcasecmp");
print_r($a);
上面的代码会产生以下输出:
Array
(
[MA1] => 0
[MA5] => 3
[MA10] => 1
[MA20] => 2
[SM8] => 5
[SM10] => 4
[SM20] => 6
[SN33] => 7
)