如何在Laravel 5.3中对包含非拉丁字符的UTF-8字符串集合进行排序? [英] How to sort a collection of UTF-8 strings containing non-Latin chars in Laravel 5.3?
本文介绍了如何在Laravel 5.3中对包含非拉丁字符的UTF-8字符串集合进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,我想按照字符串嵌套的collection
排序 字母顺序:
Folks, I want to sort following nested collection
by string alphabeticaly:
$collection = collect([
["name"=>"maroon"],
["name"=>"zoo"],
["name"=>"ábel"],
["name"=>"élof"]
])->sortBy("name");
我希望:
1=> "ábel"
2=> "élof"
3=> "maroon"
4=> "zoo"
我得到了:
1=> "maroon"
2=> "zoo"
3=> "ábel"
4=> "élof"
我看到了一些PHP线程,但是我很好奇是否有任何Laravel解决方法.谢谢.
I seen some PHP threads for this, but I am curious if there is any Laravel workaround for this. Thanks.
推荐答案
这是一种可靠的方法:
$blank = array();
$collection = collect([
["name"=>"maroon"],
["name"=>"zoo"],
["name"=>"ábel"],
["name"=>"élof"]
])->toArray();
$count = count($collection);
for ($x=0; $x < $count; $x++) {
$blank[$x] = $collection[$x]['name'];
}
$collator = collator_create('en_US');
var_export($blank);
collator_sort( $collator, $blank );
var_export( $blank );
dd($blank);
输出:
array (
0 => 'maroon',
1 => 'zoo',
2 => 'ábel',
3 => 'élof',
)array (
0 => 'ábel',
1 => 'élof',
2 => 'maroon',
3 => 'zoo',
)
Laravel漂亮输出:
array:4 [
0 => "ábel"
1 => "élof"
2 => "maroon"
3 => "zoo"
]
供个人阅读和参考: http://php.net/manual/zh/class.collator.php
For personal Reading and reference: http://php.net/manual/en/class.collator.php
希望这个答案有帮助,对不起,您的回复很晚=)
Hope this answer helps, sorry for late response =)
这篇关于如何在Laravel 5.3中对包含非拉丁字符的UTF-8字符串集合进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文