我已更新了我的问题所以请检查一下。
我有一个如下所示的数组:
$array = [
0 => [
"term" => "DECATHLON",
"count" => 7,
],
1 => [
"term" => "babywalz",
"count" => 6,
],
2 => [
"term" => "Douglas",
"count" => 3,
],
3 => [
"term" => "NETFLIX",
"count" => 2,
],
4 => [
"term" => "zalando",
"count" => 2,
],
5 => [
"term" => "Ernsting's family",
"count" => 1,
],
6 => [
"term" => "Spotify",
"count" => 1,
],
7 => [
"term" => "eventim",
"count" => 1,
]
];
我想像这样排序数组:
$array = [
1 => [
"term" => "babywalz",
"count" => 6,
],
0 => [
"term" => "DECATHLON",
"count" => 7,
],
2 => [
"term" => "Douglas",
"count" => 3,
],
5 => [
"term" => "Ernsting's family",
"count" => 1,
],
7 => [
"term" => "eventim",
"count" => 1,
],
3 => [
"term" => "NETFLIX",
"count" => 2,
],
6 => [
"term" => "Spotify",
"count" => 1,
],
];
任何建议都会有所帮助。我使用了asort(),但它不能正常工作。我也试过natcasesort()。
答案 0 :(得分:4)
感谢您的宝贵帮助。我已经解决了我的问题如下:
usort($value, function($x, $y) {
return strcasecmp($x['term'], $y['term']);
});
答案 1 :(得分:1)
这将对您的数组进行非大小写排序:
natcasesort($array);
所以:
natcasesort($array);
echo "Natural order sorting (case-insensitive):\n";
print_r($array);
答案 2 :(得分:0)
您必须使用natcasesort
,查找更多详情here
$array = [ 'D', 'a', 'Z', 'f'];
natcasesort ($array);
print_r($array);
输出
Array ( [1] => a [0] => D [3] => f [2] => Z )
答案 3 :(得分:0)
您应该将usort
与custom user function
usort($array, function($a, $b){
return strnatcmp (strtolower($a["term"]), strtolower($b["term"]));
});
答案 4 :(得分:-1)
使用函数 sort(),如下所示:sort($ array)。
请参阅文档:array sorting
答案 5 :(得分:-1)
natcasesort($array);
自然顺序排序(不区分大小写)