77

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
}
2
  • Does every item has a number?
    – Gumbo
    Commented Jun 2, 2011 at 21:24
  • foreach is a php construct, and doesn't have any items - arrays do. using count($array) returns the number of elements in it. Commented Jun 2, 2011 at 21:31

11 Answers 11

152

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
}
1
  • i just did $index+1 for the row number, exactly what i need, thanks!
    – kapitan
    Commented Feb 16, 2022 at 1:57
51

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

Just use count($Contents).

1
  • Count() works fine but for counting how many rows were supposed to be inserted or updated based on the query but suppose one or more failed for some reason? In that event, I would expect the count would be false.
    – user5175034
    Commented Aug 13, 2022 at 21:42
22
count($Contents);

or

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

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
}
1
  • var in PHP is wrong. (this isn't a JavaScript question) Commented Sep 6, 2016 at 13:18
2

Try:

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

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

also this

$count = 0;
foreach($Contents as $items) {
  $count++;
  $items[number];
}
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
1
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.

0
0

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.

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

          $data[] = array(

                'id' =>$index

         );
           
        
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.