How to sort a collection of UTF-8 strings containing non-Latin chars in Laravel 5.3?

假如想象 提交于 2020-07-08 13:04:42

问题


Folks, I want to sort following nested collection by string alphabeticaly:

$collection = collect([
    ["name"=>"maroon"],
    ["name"=>"zoo"],
    ["name"=>"ábel"],
    ["name"=>"élof"]
])->sortBy("name");

I would expect:

1=> "ábel"
2=> "élof"
3=> "maroon"
4=> "zoo"

I got instead:

1=> "maroon"
2=> "zoo"
3=> "ábel"
4=> "élof"

I seen some PHP threads for this, but I am curious if there is any Laravel workaround for this. Thanks.


回答1:


Here's a Solid way to do it:

$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);

Outputs:

array (
  0 => 'maroon',
  1 => 'zoo',
  2 => 'ábel',
  3 => 'élof',
)array (
  0 => 'ábel',
  1 => 'élof',
  2 => 'maroon',
  3 => 'zoo',
)

Laravel Pretty Output:

array:4 [
  0 => "ábel"
  1 => "élof"
  2 => "maroon"
  3 => "zoo"
]

For personal Reading and reference: http://php.net/manual/en/class.collator.php

Hope this answer helps, sorry for late response =)




回答2:


Well, I had this problem and I was able to solve it like this:

$list = $Company->administrator->sortBy(function($adm){
   return iconv('UTF-8', 'ASCII//TRANSLIT', $adm->person->name);
});

My environment was Laravel 5.5 and PHP 7.1




回答3:


You don't need to use the Collator class in this case. Laravel's collection sortBy uses asort() and arsort() internally, which has the SORT_LOCALE_STRING flag for sorting according to the currently set locale.

So, your example can be written as follows:

setlocale(LC_COLLATE, 'fr_FR.utf8'); // No need to set this if you're doing it elsewhere

$collection = collect([
    ["name"=>"maroon"],
    ["name"=>"zoo"],
    ["name"=>"ábel"],
    ["name"=>"élof"]
])->sortBy("name", SORT_LOCALE_STRING); // Signals to arsort() to take locale into consideration

This also means you don't have to convert back-and-forth from a generic PHP array to a Laravel collection.



来源:https://stackoverflow.com/questions/39374842/how-to-sort-a-collection-of-utf-8-strings-containing-non-latin-chars-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!