foreach保证迭代在PHP中的数组顺序? [英] Is foreach guaranteed to iterate in the array order in php?

查看:87
本文介绍了foreach保证迭代在PHP中的数组顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数组传递给 foreach 时,输出顺序保证与输入相同吗?我知道在某些语言中,输出顺序不能保证,因为每个元素同时被处理。

例如,我将一个有序数组传递给 foreach 写入文件。我想确保数组将被排序在输出文件中。

解决方案

以foreach它将迭代以相同的顺序。在你的情况下,数组将被排序在输出文件中。


When an array is passed to foreach is the output order guaranteed to be the same as the input? I know in some languages that the output order is not guaranteed since each element is processed at the same time.

For example, I'm passing a sorted array to foreach to write to a file. I want to be sure that the array will be sorted in the output file.

解决方案

Yes ...whatever order you will give to foreach it will iterate in same orders. And in your case array will be sorted in the output file.

这篇关于foreach保证迭代在PHP中的数组顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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