处理字母数字字符时,ksort产生错误的结果 [英] ksort produces wrong result when dealing with alphanumeric characters

查看:93
本文介绍了处理字母数字字符时,ksort产生错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

    $a = array(
        'a'=>'7833',
        'd'=>'1297',
        'c'=>'341',
        '1'=>'67',
        'b'=>'225',
        '3'=>'24',
        '2'=>'44',
        '4'=>'22',
        '0'=>'84'
    );

    ksort($a);

    print_r($a);

上面的代码产生以下输出.

The above code produces the following output.

Array
(
    [0] => 84
    [a] => 7833
    [b] => 225
    [c] => 341
    [d] => 1297
    [1] => 67
    [2] => 44
    [3] => 24
    [4] => 22
)

为什么 ksort 错误的结果?

推荐答案

您将要使用SORT_STRING标志. SORT_REGULAR会将项目与它们当前的类型进行比较,在这种情况下,数字1确实在字符串'a'之后:

You'll want to use the SORT_STRING flag. SORT_REGULAR would compare items with their current types, in which case the number 1 does come after the string 'a':

php -r "echo 1 > 'a' ? 'yes' : 'no';" // yes

这篇关于处理字母数字字符时,ksort产生错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆