计算前循环中的迭代次数

2022-08-30 08:43:22

如何计算一个前叉中有多少个项目?

我想计算总行数。

foreach ($Contents as $item) {
    $item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}

答案 1

如果只想找出数组中元素的数量,请使用 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
}

答案 2

您无需在 .foreach

只需使用.count($Contents)


推荐