计算前循环中的迭代次数
如何计算一个前叉中有多少个项目?
我想计算总行数。
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}
如何计算一个前叉中有多少个项目?
我想计算总行数。
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}
如果只想找出数组中元素的数量,请使用 count
。现在,回答您的问题...
如何计算一个前叉中有多少个项目?
$i = 0;
foreach ($Contents as $item) {
$item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
$i++;
}
如果只需要循环内的索引,可以使用
foreach($Contents as $index=>$item) {
// $index goes from 0 up to count($Contents) - 1
// $item iterates over the elements
}
您无需在 .foreach
只需使用.count($Contents)