排序数组键升序 [英] Sort array keys ascending

查看:78
本文介绍了排序数组键升序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样给数组赋值:

If I assign values to array like this:

$foo[0] = 2;
$foo[1] = 3;
print_r($foo);

我得到:

Array
(
    [0] => 2
    [1] => 3
)

但如果我这样做:

$foo[1] = 3;
$foo[0] = 2 ;
print_r($foo);

我得到:

Array
(
    [1] => 3
    [0] => 2
)

如您所见,首先进入索引为1的数组,这使我感到困惑,是否有可能使它从0开始

As you can see first goes array with index 1 and it confuses me, is it possible to make that it would start from 0

如果您有兴趣,我将值分配给索引为1的数组,因为我需要使用该值来计算索引为0的数组

推荐答案

尝试使用 ksort(); .它将您的按键升序排序

try to use ksort();. It sorts your keys ascending

<?php
$foo[1] = 3;
$foo[0] = 2 ;
ksort($foo);
print_r($foo);

产生

Array ( 
   [0] => 2 
   [1] => 3 
) 

演示

这篇关于排序数组键升序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆