基本上,它应当按数字顺序从A到Z进行分类,但使用混合型功能则不知道如何分类和随机结果。
手册中有大量警告说:
Be careful when sorting arrays with mixed types values because sort()
can produce unpredictable results.
您可以增加职能的一个参数:
缩略语
缩略语
缩略语
SORT_LOCALE_STRING - compare items as strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2, it uses the system locale, which can be changed using setlocale().
根据手册here, php.net
Edit 1:
Probably you can obtain the best sort result using the flag SORT_REGULAR
because it does not change the variable type and numbers remain numbers and strings remain strings but it will also give you a strange result
fruits[0] = 121
fruits[1] = Lemon
fruits[2] = apple
fruits[3] = banana
fruits[4] = lemon
fruits[5] = 20
fruits[6] = 40
fruits[7] = 50
I think because it compare the ascii code from the letters of strings and L
is before a b l
...
121 is in first place because you wrote it like a string "121"
<><>><>>>>
进行这项工作的最佳方式是区分这些类型:(这样,网站将把“121”作为编号而不是说明,但你可以简单地通过if
条款”决定。
<?php
$fruits = array("lemon","Lemon", 20, "banana", "apple","121",40,50);
$arr1=array();
$arr2=array();
foreach($fruits as $key=>$val){
if (is_numeric($val))
array_push($arr1,$val);
else
array_push($arr2,$val);
}
sort($arr1,SORT_NUMERIC);
sort($arr2,SORT_LOCALE_STRING);
$fruits = array_merge($arr1,$arr2);
echo "<pre>";
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "
";
}
?>