問題描述
當一個數(shù)組被傳遞給 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.
例如,我將一個已排序的數(shù)組傳遞給 foreach
以寫入文件.我想確保數(shù)組將在輸出文件中排序.
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.
這篇關(guān)于foreach 是否保證在 php 中按數(shù)組順序迭代?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!