数组排序键数字帮助


Array sort keys numerically help

$test1[2] = "one";
$test2[1] = "two";
$test2[3] = "three";
$test = $test1 + $test2;
print_r($test);

我使用了数组联合运算符,但是当我打印数组时,它的顺序错误。

Array ( [2] => one [1] => two [3] => three ) 

如何对数组中的键进行数字排序?所以我得到了下面的结果:

Array ( [1] => two [2] => one [3] => three ) 

有很多选择,这取决于你想要的结果。最简单的是ksort:

$test1[2] = "one";
$test2[1] = "two";
$test2[3] = "three";
$test = $test1 + $test2;
ksort($test);
print_r($test);

参见文档:http://www.php.net/manual/de/function.ksort.php

Try ksort: http://php.net/manual/en/function.ksort.php

print_r($test);
ksort($test);  // (sorts in place)
print_r($test);

如何对数组中的键进行数字排序?

ksort()


请,请,请,请花点时间在php.net上浏览丰富的文档(不仅仅是数组排序函数),然后再问这里。您经常会发现,我们不仅记录了函数及其功能,而且还给您提供了很好的示例来演示其用法,甚至还记录了怪异之处、复杂性和特殊注意事项。