本文介紹了計(jì)算 foreach 循環(huán)中的迭代次數(shù)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何計(jì)算一個(gè)foreach中有多少個(gè)項(xiàng)目?
How to calculate how many items in a foreach?
我想計(jì)算總行數(shù).
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}
推薦答案
如果你只想找出數(shù)組中元素的數(shù)量,使用 count
.現(xiàn)在,回答您的問題...
If you just want to find out the number of elements in an array, use count
. Now, to answer your question...
如何計(jì)算一個(gè)foreach中有多少個(gè)項(xiàng)目?
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++;
}
如果你只需要循環(huán)內(nè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
}
這篇關(guān)于計(jì)算 foreach 循環(huán)中的迭代次數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!