简体   繁体   English

如何在PHP中对带有特殊字符或数字的数组进行排序?

[英]How to sort array with special characters or numbers in PHP?

I having problems on sorting array.我在排序数组时遇到问题。 My code works on numbers but is not working well on special characters.我的代码适用于数字,但不适用于特殊字符。 I tried also with strcmp() function but no luck.我也尝试过 strcmp() 函数,但没有运气。 Here is my code:这是我的代码:

$sortBy = $this->input->get('sortBy') !== NULL ? $this->input->get('sortBy') : "";
$arr_ongoing = $this->getArr($this->array_ongoing, "id");
setlocale(LC_ALL, "fr-FR");
usort($arr_ongoing, function ($a, $b) use($sortBy) {
    return strcoll($a[$sortBy], $b[$sortBy]); 
});

here is my sample array: Array ( 1 => Array ( [arrete_id] => 1 [etude_id] => 458 [lastname] => Chambre test [crpcen] => 29000 [ville] => Quimper )这是我的示例数组: Array ( 1 => Array ( [arrete_id] => 1 [etude_id] => 458 [lastname] => Chambre test [crpcen] => 29000 [ville] => Quimper)

[2] => Array
    (
        [arrete_id] => 2
        [etude_id] => 361
        [lastname] => PICART et Associé(s)
        [crpcen] => 44007
        [ville] => NANTES
    )

[3] => Array
    (
        [arrete_id] => 3
        [etude_id] => 35
        [lastname] => JARNOUEN de VILLARTAY et REGEON-VERGNOUX - SE
        [crpcen] => 22005
        [ville] => SAINT-BRIEUC CEDEX 2
    )

[12] => Array
    (
        [arrete_id] => 12
        [etude_id] => 132
        [lastname] =>  LE PAPE et LACOURT
        [crpcen] => 29022
        [ville] => PONT-L'ABBE CEDEX
    )

[13] => Array
    (
        [arrete_id] => 13
        [etude_id] => 222
        [lastname] => KERJEAN et Associé(s)
        [crpcen] => 35129
        [ville] =>  BRUZ CEDEX
    )

)

输出

If by "special chars" you mean chars specific to a particular language I suggest using Collator from the intl package of extensions.如果“特殊字符”是指特定于特定语言的字符,我建议使用intl扩展包中的Collator

For example for Polish standard sort of:例如对于波兰标准sort

$array = [ 'a', 'ą', 'b', 'z' ];

will give you:会给你:

array(4) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "b"
  [2] =>
  string(1) "z"
  [3] =>
  string(2) "ą"
}

while sorting using Collator the correct one:在使用 Collat​​or 进行排序时,正确的是:

$collator = new Collator('pl_PL');
$collator->sort($array);

gives the correct:给出正确的:

array(4) {
  [0] =>
  string(1) "a"
  [1] =>
  string(2) "ą"
  [2] =>
  string(1) "b"
  [3] =>
  string(1) "z"
}

If you cannot use the pecl intl but using PHP >=7.0.0 you can use this lib: https://github.com/voku/portable-utf8如果你不能使用 pecl intl但使用 PHP >=7.0.0 你可以使用这个库: https : //github.com/voku/portable-utf8

for example:例如:

$array = [ 'a', 'ą', 'b', 'z' ];

function mysort($a, $b) {
    return UTF8::strcmp($a, $b);
}

use voku\helper\UTF8;
usort($array, 'mysort');


It does not require mbstring or intl to be installed (although suggests it).它不需要安装 mbstring 或 intl(尽管建议安装)。

You should not rely on setlocale as it is based on the locales installed in the particular system and those may not only be not installed, but also their names may differ (between Windows and *nix, but also between *nixes).您不应该依赖setlocale因为它基于安装在特定系统中的语言环境,而且这些语言环境可能不仅没有安装,而且它们的名称可能不同(在 Windows 和 *nix 之间,以及在 *nix 之间)。

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

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