Count number of iterations in a foreach loop

PhpForeach

Php Problem Overview


How to calculate how many items in a 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
}

Php Solutions


Solution 1 - Php

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

> 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++;
}

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
}

Solution 2 - Php

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

Just use count($Contents).

Solution 3 - Php

count($Contents);

or

sizeof($Contents);

Solution 4 - Php

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

Solution 5 - Php

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.

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

Solution 6 - Php

Try:

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

Solution 7 - Php

$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

Solution 8 - Php

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

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

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

Solution 9 - Php

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

also this

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

Solution 10 - Php

Imagine a counter with an initial value of 0.

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

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

$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.

Solution 11 - Php

    $index = 0;
	foreach( $array ?? [] as  $index=> $item  ) {
		 $index++;

	      $data[] = array(

	      		'id' =>$index

	     );
	       
	    
	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyuli chikaView Question on Stackoverflow
Solution 1 - PhpaioobeView Answer on Stackoverflow
Solution 2 - PhptjmView Answer on Stackoverflow
Solution 3 - PhpgpreslandView Answer on Stackoverflow
Solution 4 - PhpAlejandro MorenoView Answer on Stackoverflow
Solution 5 - PhpJimPView Answer on Stackoverflow
Solution 6 - PhpvivekpvkView Answer on Stackoverflow
Solution 7 - PhpwebbiedaveView Answer on Stackoverflow
Solution 8 - Phpstatistnr1View Answer on Stackoverflow
Solution 9 - PhpJourney DagocView Answer on Stackoverflow
Solution 10 - PhpVictor LangatView Answer on Stackoverflow
Solution 11 - PhpMaicol RomeroView Answer on Stackoverflow