加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 数组排序键数字帮助

发布时间:2020-12-13 22:02:45 所属栏目:PHP教程 来源:网络整理
导读:$test1[2] = "one";$test2[1] = "two";$test2[3] = "three";$test = $test1 + $test2;print_r($test); 我已经使用了数组联合运算符,但是当我打印数组时,它的顺序错误. Array ( [2] = one [1] = two [3] = three ) 如何在数组中以数字方式对键进行排序?所以
$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 )

解决方法

有一个 number of options,取决于你所追求的结果.最简单的是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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读