计算foreach循环中的迭代次数

时间:2021-01-01 20:23:06

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
}

Thanks.

谢谢。

9 个解决方案

#1


93  

First of all, 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) {
    $i++;
    $item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}

You could also have a look at the answers over here:

你也可以看看这里的答案:

#2


40  

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

你不需要在foreach中做到这一点。

Just use count($Contents).

只需使用count($ Contents)。

#3


15  

sizeof($Contents);

or

要么

count($Contents);

#4


9  

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

#5


4  

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
}

#6


1  

$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

#7


1  

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中,而不是在上面。

#8


1  

Try:

尝试:

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

#9


0  

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

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

also this

这也是

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

#1


93  

First of all, 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) {
    $i++;
    $item[number];// if there are 15 $item[number] in this foreach, I want get the value : 15
}

You could also have a look at the answers over here:

你也可以看看这里的答案:

#2


40  

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

你不需要在foreach中做到这一点。

Just use count($Contents).

只需使用count($ Contents)。

#3


15  

sizeof($Contents);

or

要么

count($Contents);

#4


9  

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

#5


4  

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
}

#6


1  

$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

#7


1  

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中,而不是在上面。

#8


1  

Try:

尝试:

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

#9


0  

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

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

also this

这也是

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