数组排序键在数字上有帮助

use*_*134 4 php

$test1[2] = "one";

$test2[1] = "two";
$test2[3] = "three";

$test = $test1 + $test2;

print_r($test);
Run Code Online (Sandbox Code Playgroud)

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

Array ( [2] => one [1] => two [3] => three ) 
Run Code Online (Sandbox Code Playgroud)

如何在数组中以数字方式对键进行排序?所以我得到以下结果.

Array ( [1] => two [2] => one [3] => three ) 
Run Code Online (Sandbox Code Playgroud)

dka*_*ins 5

尝试ksort:http://php.net/manual/en/function.ksort.php

print_r($test);
ksort($test);  // (sorts in place)
print_r($test);
Run Code Online (Sandbox Code Playgroud)


Chr*_*ker 5

多种选择,具体取决于您追求的结果。最简单的是ksort

$test1[2] = "one";

$test2[1] = "two";
$test2[3] = "three";

$test = $test1 + $test2;
ksort($test);
print_r($test);
Run Code Online (Sandbox Code Playgroud)

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