tags:

views:

54

answers:

1
$count=0;
$numb=50;
foreach ($sepkeys as $dbkey)
{
    for ($page=10;$page<=$numb;$page=$page + 10)
    {
    // the if block

$count=$count+1;

}

}

I am trying to maintain a separate a count for each key number in the above code.

Eg: key- 574, it searches from pages 10-50 and increments the count.

The problem that I have is the count is continuous. After searching for the first key and moves on to the next key and then I need the count to start from the beginning rather than being continuous.

Eg: key-874 : count = 22, in my case the next key 875 : the count is 23

I need to make it 1.

I removed the if block and several lines because the code is too long.

Can someone please suggest me a way how to do it

+1  A: 

Use an array for storing count values:

$count = array();
$numb=50;
foreach ($sepkeys as $dbkey)
{
    for ($page=10;$page<=$numb;$page=$page + 10)
    {
         // the if block
         $count[$dbkey] = $count[$dbkey] + 1;
    }
}

//Displaying counts:
foreach($count as $key=>$val)
    echo "Key {$key} has count: {$val}".PHP_EOL;

This way, every key value will have count stored in the array and you have separate count for separate keys.

shamittomar
not sure if in php you'd need to add $count[$dbkey] = 0; before the nested for loop. but it's safer.
Anand
@Anand, No. $count[$dbkey] = 0; is not required.
shamittomar