laravel 数组元素按中文排序
1、按英文排序
$r = [
['color'=>'b', 'color_zh'=>'波'],
['color'=>'c', 'color_zh'=>'吃'],
['color'=>'a', 'color_zh'=>'啊'],
['color'=>'d', 'color_zh'=>'得'],
];
//按英文排序
$rs = collect($r)->sortBy('color')
->values()
->all();
2、按中文排序
//按中文排序
$collator = new Collator('zh');
$rs = collect($r)
->sortBy(function ($item, $key) use ($collator) {
return $collator->getSortKey($item['color_zh']);
})
->values()
->all();
3、输出结果
array:4 [
0 => array:2 [
"color" => "a"
"color_zh" => "啊"
]
1 => array:2 [
"color" => "b"
"color_zh" => "波"
]
2 => array:2 [
"color" => "c"
"color_zh" => "吃"
]
3 => array:2 [
"color" => "d"
"color_zh" => "得"
]
]