I have some simple code as sample where I am trying to count how many items are in a foreach loop, There are 2 items in the loop but when I echo the $count out it shows 11 instead of 2. I would be grateful if someone could help with this. Many thanks.
<?php
$count = 0;
foreach ($boxitem as $items) {
$count++;
echo $items . '<br />'; ---> This outputs correct number of items
}
echo $count;
?>
Based on the debugging output, the posted code isn’t the whole code. It’s being executed inside of some other loop that’s producing two separate single element arrays consisting of the strings.
as @mabismad says, your code output indicates you’re running a foreach on something that you think is an array of size 11, but is actually 11 arrays of size 1.