本文介紹了計算 foreach 循環中的迭代次數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何計算一個foreach中有多少個項目?
How to calculate how many items in a foreach?
我想計算總行數.
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}
推薦答案
如果你只想找出數組中元素的數量,使用 count
.現在,回答您的問題...
If you just want to find out the number of elements in an array, use count
. Now, to answer your question...
如何計算一個foreach中有多少個項目?
How to calculate how many items in a foreach?
$i = 0;
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
$i++;
}
如果你只需要循環內的索引,你可以使用
If you only need the index inside the loop, you could use
foreach($Contents as $index=>$item) {
// $index goes from 0 up to count($Contents) - 1
// $item iterates over the elements
}
這篇關于計算 foreach 循環中的迭代次數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!