php 计算 foreach 循环中的迭代次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6220546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:41:17  来源:igfitidea点击:

Count number of iterations in a foreach loop

phpforeach

提问by yuli chika

How to calculate how many items in a foreach?

如何计算foreach中有多少个项目?

I want to count total rows.

我想计算总行数。

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

回答by aioobe

If you just want to find out the number of elements in an array, use count. Now, to answer your question...

如果您只想找出数组中元素的数量,请使用count. 现在,回答你的问题...

How to calculate how many items in a foreach?

如何计算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
}

回答by tjm

You don't need to do it in the foreach.

您不需要在foreach.

Just use count($Contents).

只需使用count($Contents).

回答by Alejandro Moreno

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

回答by gpresland

count($Contents);

or

或者

sizeof($Contents);

回答by JimP

There's a few different ways you can tackle this one.

有几种不同的方法可以解决这个问题。

You can set a counter before the foreach() and then just iterate through which is the easiest approach.

您可以在 foreach() 之前设置一个计数器,然后迭代这是最简单的方法。

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

回答by webbiedave

$Contents = array(
    array('number'=>1), 
    array('number'=>2), 
    array('number'=>4), 
    array('number'=>4), 
    array('number'=>4), 
    array('number'=>5)
);

$counts = array();

foreach ($Contents as $item) {
    if (!isset($counts[$item['number']])) {
        $counts[$item['number']] = 0;
    }
    $counts[$item['number']]++;
}

echo $counts[4]; // output 3

回答by vivekpvk

Try:

尝试:

$counter = 0;
foreach ($Contents as $item) {
          something 
          your code  ...
      $counter++;      
}
$total_count=$counter-1;

回答by Journey Dagoc

You can do sizeof($Contents)or count($Contents)

你可以做sizeof($Contents)count($Contents)

also this

还有这个

$count = 0;
foreach($Contents as $items) {
  $count++;
  $items[number];
}

回答by statistnr1

foreach ($array as $value)
{       
    if(!isset($counter))
    {
        $counter = 0;
    }
    $counter++;
}

//Sorry if the code isn't shown correctly. :P

//对不起,如果代码没有正确显示。:P

//I like this version more, because the counter variable is IN the foreach, and not above.

//我更喜欢这个版本,因为计数器变量在foreach里面,而不是上面。

回答by Victor Langat

Imagine a counter with an initial value of 0.

想象一个初始值为 的计数器0

For every loop, increment the counter value by 1 using $counter = 0;

对于每个循环,使用以下方法将计数器值加 1 $counter = 0;

The final counter value returned by the loop will be the number of iterations of your for loop. Find the code below:

循环返回的最终计数器值将是 for 循环的迭代次数。找到下面的代码:

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

Try that.

试试那个。