PHP非大小写敏感排序不起作用

时间:2013-05-04 18:07:48

标签: php

以下代码应该将Banana放在苹果之后,但它会结束。

$json='{"apple":null,"orange":["s","s2"],"pear":null}';

$json=json_decode($json,true);

$json['Banana']=NULL;

ksort ($json, SORT_NATURAL | SORT_FLAG_CASE);

print_r($json);

2 个答案:

答案 0 :(得分:0)

SORT_NATURALSORT_FLAG_CASE仅适用于php版本> 5.4.0

从这里: http://www.php.net/manual/en/function.sort.php

中的

5.4.0   Added support for SORT_NATURAL and SORT_FLAG_CASE as sort_flags

答案 1 :(得分:0)

function natksort(&$array) {
    $keys = array_keys($array);
    natcasesort($keys);

    foreach ($keys as $k) {
        $new_array[$k] = $array[$k];
    }

    $array = $new_array;
    return true;
}

$json='{"apple":null,"orange":["s","s2"],"pear":null}';
$json=json_decode($json,true);
$json['Banana']=NULL;

natksort($json);

print_r($json);

结果:

Array
(
    [apple] => 
    [Banana] => 
    [orange] => Array
        (
            [0] => s
            [1] => s2
        )

    [pear] => 
)