我正在寻找一个自定义函数,它将尊重数组的顺序并按“名称”索引对其进行排序。'strcasecmp()' 函数无法理解人类会读到的字母数字值。它认为“Apples 12”的价值低于“Apples 5”。我尝试了这种方法,但找不到比较字母数字值的函数:
$array = array(
0 => array(
'id' => 2,
'type' => 'Apples',
'name' => 'Apples 5',
),
1 => array(
'id' => 3,
'type' => 'Grapes',
'name' => 'Apples',
),
2 => array(
'id' => 4,
'type' => 'Apples',
'name' => 'Apples 4',
),
3 => array(
'id' => 5,
'type' => 'Grapes',
'name' => 'Apples 01',
),
4 => array(
'id' => 6,
'type' => 'Apples',
'name' => 'Apples 1',
),
5 => array(
'id' => 7,
'type' => 'Grapes',
'name' => 'Apples 12',
)
);
uasort($array, function($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
foreach($array as $single) {
echo $single['name'].'<br />';
}
上面代码的意外结果:
Apples
Apples 01
Apples 1
Apples 12
Apples 4
Apples 5
我想要达到的结果:
Apples
Apples 01
Apples 1
Apples 4
Apples 5
Apples 12
有任何想法吗?