PHP foreach循环需要数组元素进行数字顺序呢? [英] PHP foreach loop takes array elements out of numeric order?

查看:280
本文介绍了PHP foreach循环需要数组元素进行数字顺序呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组我建立这样的:

I have an array I am building like this:

foreach($items as $item) {
    $this->_array[(int)$item->getPosition()] = $item;
}

当我再通过该数组输出运行它,我希望这样的:

When I then run through that array to output it, I expect this:

array (
    [0] => item0,
    [1] => item1,
    [2] => item2,
    [3] => item3,
)

不过,我得到这样的:

But I get this:

array (
    [3] => item3,
    [0] => item0,
    [2] => item2,
    [1] => item1,
)

这点我只能假设是按键分别设置的顺序。他们为什么不出来,以便?

Which I can only assume is the order the keys were set in. Why aren't they coming out in order?

有没有办法迫使阵列通过数字键才能购买?

Is there a way to force the array to order by keys in numeric order?

推荐答案

只是 ksort()数组第一位。

这篇关于PHP foreach循环需要数组元素进行数字顺序呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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