techie_28
techie_28

Reputation: 2133

Restarting a PHP Loop

I have this piece of code

foreach($mnthArrPtrn as $m => $mn)
{   
    if(!isset($catName)) {
        $catVals = array();      
        $prevCat = $catName = $pntChrtQry[0]['CAT']['categoryname']; 
        $pntVals .= '{name:'.$catName.',data:[';
    }else if($prevCat != $catName) {
        $prevCat = $catName;
        $catVals = array(); 
        $pntVals .= '{name:'.$catName.',data:[';
    }     
    foreach($pntChrtQry as $key => $val){
        $catName = $val['CAT']['categoryname'];
        if($prevCat != $catName){
            continue 2;
        }
        echo '<br />$m::'.$m; 
        echo '<br />$mn::'.$mn;
        echo '<br />$val::'.$val[0]['MNTH'];
        if($m == $val[0]['MNTH'] || $mn == $val[0]['MNTH']){
            $catVals[] = $val[0]['total'];
        }
    }
    pr($catVals);
    if(!isset($catName)){
        $pntVals .= ']},';
    }
    $catName = $val['CAT']['categoryname'];
}

1st loop iterates over a months array which are joined as a key value pair. What I am doing here is on getting a new catName I continue the internal loop but at the same time I want to restart loop 1 with $prevCat,$catName still preserving their values. Is this possible? Sorry If this is a silly question.

I tried converting the first one to a while statement and use a reset then but It didn't help me.

Upvotes: 0

Views: 4161

Answers (1)

halfer
halfer

Reputation: 20439

Something like this will allow you to arbitrarily restart a loop:

while (list($key, $value) = each($mnthArrPtrn)) {
    if ($needToRestart) {
        reset($mnthArrPtrn);
    }
}

See more here.

Upvotes: 4

Related Questions