0

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.

13
  • a) give your variables, good names like $month, $months, $category, $values, etc. b) Break this up into multiple clear functions, this code drives one crazy to read. c) If you want to convert a PHP array to a JSON array, use the native functions for that. Commented Feb 3, 2013 at 11:54
  • Isnt there a way to reset the extrnl loop? Commented Feb 3, 2013 at 11:59
  • If you need to restart a loop, use while in conjunction with each, with a reset to go back to the start of an array. You can't restart a foreach, no. Commented Feb 3, 2013 at 12:00
  • is that possible to do right above the continue statement?I want to restart the extrnl one Commented Feb 3, 2013 at 12:01
  • 1
    @techie_28 form the required array in PHP first, then use json_encode. (and keep working on improving your english, it will help you greatly!) Commented Feb 3, 2013 at 12:49

1 Answer 1

4

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

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

See more here.

Sign up to request clarification or add additional context in comments.

7 Comments

I have a nested loop inside the external one..And on that continue I want to restart...will that work/
Yes, I think so. Swap the outer loop for the one I suggest, and reset the array before you do continue 2.
Perhaps your continue should be at the end of your inner loop? I don't know, since I don't know the algorithm, nor the purpose of the code.
That didnt helped too @halfer I tried the outer foreach to be while(list($m,$mn) = each($mnthArrPtrn))
Well, this is where you have to do some debugging. It's probably worth my pointing out that (in general) if you respond with "That didn't help too", there's not much I can say unless you can explain in detail how it didn't help. Otherwise I have to guess, which isn't very useful! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.