本文介紹了PHP - 使用 foreach 抓取第一個元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
想知道在 foreach 循環中獲得第一次迭代的好方法是什么.我想在第一次迭代中做一些不同的事情.
Wondering what would be a good method to get the first iteration on a foreach loop. I want to do something different on the first iteration.
在這些情況下,有條件是我們最好的選擇嗎?
Is a conditional our best option on these cases?
推薦答案
是的,如果您無法以不同的方式(正常的 for 循環)遍歷對象,在這種情況下只需使用條件:>
Yes, if you are not able to go through the object in a different way (a normal for loop), just use a conditional in this case:
$first = true;
foreach ( $obj as $value )
{
if ( $first )
{
// do something
$first = false;
}
else
{
// do something
}
// do something
}
這篇關于PHP - 使用 foreach 抓取第一個元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!