sorting - laravel 5.3 collection sort UTF8 strings -
folks, want sort following nested collection
by string alphabeticaly:
$collection = collect([ ["name"=>"maroon"], ["name"=>"zoo"], ["name"=>"ábel"], ["name"=>"élof"] ])->sortby("name");
i expect:
1=> "ábel" 2=> "élof" 3=> "maroon" 4=> "zoo"
i got instead:
1=> "maroon" 2=> "zoo" 3=> "ábel" 4=> "élof"
i seen php threads this, curious if there laravel workaround this. thanks.
here's solid way it:
$blank = array(); $collection = collect([ ["name"=>"maroon"], ["name"=>"zoo"], ["name"=>"ábel"], ["name"=>"élof"] ])->toarray(); $count = count($collection); ($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 , reference: http://php.net/manual/en/class.collator.php
hope answer helps, sorry late response =)
Comments
Post a Comment