yuli chika yuli chika - 3 months ago 15
PHP Question

Count number of iterations in a foreach loop

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
}


Thanks.

Answer

First of all, 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) {
    $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: