Is foreach guaranteed to iterate in the array order in php?(foreach 是否保证在 php 中按数组顺序迭代?)
问题描述
当一个数组被传递给 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.
例如,我将一个已排序的数组传递给 foreach
以写入文件.我想确保数组将在输出文件中排序.
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.
推荐答案
是的...无论你给 foreach 提供什么顺序,它都会以相同的顺序迭代.在您的情况下,array 将在输出文件中排序.
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 中按数组顺序迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:foreach 是否保证在 php 中按数组顺序迭代?


基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01