简体   繁体   English

如何在php中使用kso​​rt对重音字母进行排序?

[英]How to sort accented letters using ksort in php?

I am working on a php code as shown below: 我正在开发一个PHP代码,如下所示:

<?php
$age=array("Bam"=>"35","Bân"=>"37","Bao"=>"43");
ksort($age);

foreach($age as $x=>$x_value)
   {
   echo "Key=" . $x . ", Value=" . $x_value;
   echo "<br>";
   }
?>

The above php code display the following o/p: 上面的php代码显示以下o / p:

Key=Bam, Value=35
Key=Bao, Value=43
Key=Bân, Value=37

Problem Statement: 问题陈述:

I am wondering what changes I should make in the php code above so that it display the following o/p: 我想知道我应该在上面的PHP代码中做出哪些更改,以便显示以下o / p:

Key=Bam, Value=35
Key=Bân, Value=37
Key=Bao, Value=43

I experimented with some of the locale-based methods suggested, but eventually the only way I was able to get it to work was to use one of the remove_accents functions from here and then sort the array according to the order of the unaccented keys. 我尝试了一些基于语言环境的方法,但最终我能够让它工作的唯一方法是使用此处remove_accents函数之一,然后根据unaccented键的顺序对数组进行排序。

$unaccented = array_map('remove_accents', array_keys($age));
array_multisort($unaccented, $age);

Doesn't mean it won't work with setlocale etc, just that I wasn't able to make it work, but this may be a viable alternative. 并不意味着它不能用于setlocale等,只是因为我无法使它工作,但这可能是一个可行的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM