本文介紹了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 中按數組順序迭代?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!