用重音排序的php数组

pet*_*ter 7 php arrays sorting

我用这个按照姓氏排序:

  usort($fb_friends['data'], "custom_sort");          
  function custom_sort($a,$b) { 
    return $a['last_name']>$b['last_name'];
  }

  foreach($fb_friends['data'] as $friend) { 
    echo '<br>'; 
    echo $friend['name']; 
  } 
Run Code Online (Sandbox Code Playgroud)

但是 - 当姓氏是重音时,例如Šiko,Áron等,这些名字就在最后.我怎样才能正确排序?

J. *_*uni 14

使用多字节字符串函数.有一个叫做的功能strcoll似乎适合你的需要.

更多信息:


编辑:在下面添加了Peter的工作代码

setlocale(LC_COLLATE, 'sk_SK.utf8');

usort($fb_friends['data'], 'custom_sort');

function custom_sort($a, $b) {
    return strcoll ($a['last_name'], $b['last_name']);
}

foreach ($fb_friends['data'] as $friend) {
    echo '<br>';
    echo $friend['name'];
}
Run Code Online (Sandbox Code Playgroud)


Luc*_*nte 9

什么是第一位的?

\n
    \n
  • A
  • \n
  • A
  • \n
  • \xe6\x96\x87
  • \n
  • \xd8\xab
  • \n
\n

这是由Collat​​ion定义的。

\n

PHP 为此提供了 Collat​​or 类:https ://www.php.net/manual/en/class.collat​​or.php

\n

例子:

\n
$array = [ \'A\', \'a\', \'\xe6\x96\x87\', \'\xd8\xab\' ];\n\n// Brazilian Portuguese\n$collator = new Collator(\'pt_BR\');\n\n$collator->asort( $array );\n\nprint_r( $array );\n\n
Run Code Online (Sandbox Code Playgroud)\n

返回:

\n
Array\n(\n    [1] => a\n    [0] => A\n    [3] => \xd8\xab\n    [2] => \xe6\x96\x87\n)\n
Run Code Online (Sandbox Code Playgroud)\n

现在有中文整理new Collator(\'zh\')

\n
Array\n(\n    [2] => \xe6\x96\x87\n    [1] => a\n    [0] => A\n    [3] => \xd8\xab\n)\n
Run Code Online (Sandbox Code Playgroud)\n

您可以在这里自己尝试一下: https: //3v4l.org/0vsBR

\n