PHP strcmp函数不按字母顺序排序

时间:2013-07-24 14:12:07

标签: php sorting strcmp usort

我有这个脚本

$todos=array(
    '0' => array('id' => 46, 'nombre' => 'b2 miami downtown','precio' => 149.01,'tipo' => 'expedia'),
    '1' => array('id' => 242,'nombre' => 'b2 Miami Downtown','precio' => 176.98,'tipo' => 'hotelbeds'),
    '2' => array('id' => 240,'nombre' => 'b2 Miami Downtown','precio' => 131.62,'tipo' => 'hotelbeds'),
    '3' => array('id' => 241,'nombre' => 'b2 Miami Downtown','precio' => 131.62,'tipo' => 'hotelbeds'),
    '4' => array( 'id' => 178,'nombre' => 'Z Ocean Hotel South Beach','precio' => 400.93,'tipo' => 'expedia'),
    '5' => array( 'id' => 254,'nombre' => 'Whitelaw Hotel','precio' => 142.29,'tipo' => 'hotelbeds'),
    '6' => array( 'id' => 124, 'nombre' => 'Whitelaw Hotel','precio' => 132.04,'tipo' => 'tourico'),
    '7' => array( 'id' => 62, 'nombre' => 'Westin Colonnade Coral Gables','precio' => 138.39,'tipo' => 'tourico'),
    '8' => array( 'id' => 275, 'nombre' => 'Westgate South Beach','precio' => 166.79,'tipo' => 'hotelbeds'),
    '9' => array( 'id' => 44, 'nombre' => 'W South Beach','precio' => 374.55,'tipo' => 'tourico'),
    '10' => array( 'id' => 206, 'nombre' => 'Holiday Inn Express West Doral Miami Airport','precio' => 103.22,'tipo' => 'hotelbeds'),
    '11' => array( 'id' => 117, 'nombre' => 'Holiday Inn Express Miami International Airport','precio' => 106.3,'tipo' => 'expedia'),
    '12' => array( 'id' => 114, 'nombre' => 'Holiday Inn Express Miami Airport Doral Area','precio' => 113,'tipo' => 'tourico'),
    '13' => array( 'id' => 78, 'nombre' => 'Holiday Inn Express Doral','precio' => 93.36,'tipo' => 'expedia'),
    '14' => array( 'id' => 62, 'nombre' => 'Holiday Inn Express & Suites Kendall East Miami','precio' => 128.46,'tipo' => 'expedia'),
    '15' => array( 'id' => 18, 'nombre' => 'Best Western Plus Kendall Hotel & Suites','precio' => 429.55,'tipo' => 'tourico'),
    '16' => array( 'id' => 19, 'nombre' => 'Best Western Plus Kendall Hotel & Suites','precio' => 429.55,'tipo' => 'expedia'),
    '17' => array( 'id' => 120, 'nombre' => 'Best Western Kendall Hotel & Suites','precio' => 114.27,'tipo' => 'expedia'),
    '18' => array( 'id' => 69, 'nombre' => 'Holiday Inn Express & Suites Kendall','precio' => 126.5,'tipo' => 'expedia'));

    echo "<pre>";
    print_r($todos);

    function comp($a,$b){
        if(soundex($a['nombre'])==soundex($b['nombre'])) return($a['precio']-$b['precio']);
        if(soundex($a['nombre'])!=soundex($b['nombre'])) return strcmp($a['nombre'], $b['nombre']);
    }
    usort($todos,'comp');
    print_r($todos);
    echo "<pre>";

我需要先按酒店名称排序数组,然后按价格(precio)对每个酒店进行排序。 使用 soundex 的原因是因为同一家酒店可能有不同的名称,具体取决于提供商(证明者),因此我需要对酒店进行整理,并先放入最低价格,以删除其他酒店所以我只能提供最优惠的价格。

输出:

Array
(
   [0] => Array
        (
            [id] => 46
            [nombre] => b2 miami downtown
            [precio] => 149.01
            [tipo] => expedia
        )

    [1] => Array
        (
            [id] => 242
            [nombre] => b2 Miami Downtown
            [precio] => 176.98
            [tipo] => hotelbeds
        )

    [2] => Array
        (
            [id] => 240
            [nombre] => b2 Miami Downtown
            [precio] => 131.62
            [tipo] => hotelbeds
        )

    [3] => Array
        (
            [id] => 241
            [nombre] => b2 Miami Downtown
            [precio] => 131.62
            [tipo] => hotelbeds
        )

    [4] => Array
        (
            [id] => 178
            [nombre] => Z Ocean Hotel South Beach
            [precio] => 400.93
            [tipo] => expedia
        )

    [5] => Array
        (
            [id] => 254
            [nombre] => Whitelaw Hotel
            [precio] => 142.29
            [tipo] => hotelbeds
        )

    [6] => Array
        (
            [id] => 124
            [nombre] => Whitelaw Hotel
            [precio] => 132.04
            [tipo] => tourico
        )

    [7] => Array
        (
            [id] => 62
            [nombre] => Westin Colonnade Coral Gables
            [precio] => 138.39
            [tipo] => tourico
        )

    [8] => Array
        (
            [id] => 275
            [nombre] => Westgate South Beach
            [precio] => 166.79
            [tipo] => hotelbeds
        )

    [9] => Array
        (
            [id] => 44
            [nombre] => W South Beach
            [precio] => 374.55
            [tipo] => tourico
        )

    [10] => Array
        (
            [id] => 206
            [nombre] => Holiday Inn Express West Doral Miami Airport
            [precio] => 103.22
            [tipo] => hotelbeds
        )

    [11] => Array
        (
            [id] => 117
            [nombre] => Holiday Inn Express Miami International Airport
            [precio] => 106.3
            [tipo] => expedia
        )

    [12] => Array
        (
            [id] => 114
            [nombre] => Holiday Inn Express Miami Airport Doral Area
            [precio] => 113
            [tipo] => tourico
        )

    [13] => Array
        (
            [id] => 78
            [nombre] => Holiday Inn Express Doral
            [precio] => 93.36
            [tipo] => expedia
        )

    [14] => Array
        (
            [id] => 62
            [nombre] => Holiday Inn Express & Suites Kendall East Miami
            [precio] => 128.46
            [tipo] => expedia
        )

    [15] => Array
        (
            [id] => 18
            [nombre] => Best Western Plus Kendall Hotel & Suites
            [precio] => 429.55
            [tipo] => tourico
        )

    [16] => Array
        (
            [id] => 19
            [nombre] => Best Western Plus Kendall Hotel & Suites
            [precio] => 429.55
            [tipo] => expedia
        )

    [17] => Array
        (
            [id] => 120
            [nombre] => Best Western Kendall Hotel & Suites
            [precio] => 114.27
            [tipo] => expedia
        )

    [18] => Array
        (
            [id] => 69
            [nombre] => Holiday Inn Express & Suites Kendall
            [precio] => 126.5
            [tipo] => expedia
        )

)

分类阵列

Array
(
    [0] => Array
        (
            [id] => 120
            [nombre] => Best Western Kendall Hotel & Suites
            [precio] => 114.27
            [tipo] => expedia
        )

    [1] => Array
        (
            [id] => 19
            [nombre] => Best Western Plus Kendall Hotel & Suites
            [precio] => 429.55
            [tipo] => expedia
        )

    [2] => Array
        (
            [id] => 18
            [nombre] => Best Western Plus Kendall Hotel & Suites
            [precio] => 429.55
            [tipo] => tourico
        )

    [3] => Array
        (
            [id] => 78
            [nombre] => Holiday Inn Express Doral
            [precio] => 93.36
            [tipo] => expedia
        )

    [4] => Array
        (
            [id] => 206
            [nombre] => Holiday Inn Express West Doral Miami Airport
            [precio] => 103.22
            [tipo] => hotelbeds
        )

    [5] => Array
        (
            [id] => 117
            [nombre] => Holiday Inn Express Miami International Airport
            [precio] => 106.3
            [tipo] => expedia
        )

    [6] => Array
        (
            [id] => 114
            [nombre] => Holiday Inn Express Miami Airport Doral Area
            [precio] => 113
            [tipo] => tourico
        )

    [7] => Array
        (
            [id] => 69
            [nombre] => Holiday Inn Express & Suites Kendall
            [precio] => 126.5
            [tipo] => expedia
        )

    [8] => Array
        (
            [id] => 62
            [nombre] => Holiday Inn Express & Suites Kendall East Miami
            [precio] => 128.46
            [tipo] => expedia
        )

    [9] => Array
        (
            [id] => 44
            [nombre] => W South Beach
            [precio] => 374.55
            [tipo] => tourico
        )

    [10] => Array
        (
            [id] => 275
            [nombre] => Westgate South Beach
            [precio] => 166.79
            [tipo] => hotelbeds
        )

    [11] => Array
        (
            [id] => 62
            [nombre] => Westin Colonnade Coral Gables
            [precio] => 138.39
            [tipo] => tourico
        )

    [12] => Array
        (
            [id] => 124
            [nombre] => Whitelaw Hotel
            [precio] => 132.04
            [tipo] => tourico
        )

    [13] => Array
        (
            [id] => 254
            [nombre] => Whitelaw Hotel
            [precio] => 142.29
            [tipo] => hotelbeds
        )

    [14] => Array
        (
            [id] => 178
            [nombre] => Z Ocean Hotel South Beach
            [precio] => 400.93
            [tipo] => expedia
        )

    [15] => Array
        (
            [id] => 240
            [nombre] => b2 Miami Downtown
            [precio] => 131.62
            [tipo] => hotelbeds
        )

    [16] => Array
        (
            [id] => 241
            [nombre] => b2 Miami Downtown
            [precio] => 131.62
            [tipo] => hotelbeds
        )

    [17] => Array
        (
            [id] => 46
            [nombre] => b2 miami downtown
            [precio] => 149.01
            [tipo] => expedia
        )

    [18] => Array
        (
            [id] => 242
            [nombre] => b2 Miami Downtown
            [precio] => 176.98
            [tipo] => hotelbeds
        )

)

正如你所看到的那样,“b2迈阿密市中心”应该排在阵列的顶端而不是最后一个。

请帮忙解决这个问题。

感谢!!!

1 个答案:

答案 0 :(得分:3)

来自strcmp的PHP手册:

  

请注意,此比较区分大小写。

字母A-Z在字母a-z之前来。请改为strcasecmp

编辑:即使使用strcasecmp,也要注意它的不区分大小写只适用于英文字符集。如果您在其他语言环境中使用名称(西班牙语,法语,德语,您可以命名),那么仍将以二进制方式比较这些非基本ASCII字符。例如,

posada el Ñandú rengo不等同于posada el ñandÚ rengo

在比较之前,您可能希望使用multibyte string函数,例如mb_convert_case或mb_strtolower。