1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I break this foreach loop and continue

Discussion in 'PHP' started by ketting00, Jul 30, 2015.

  1. #1
    Hi guys,

    I've an array I want to split in two parts. I've tried this and it isn't working.
    
    foreach($array as $key => $value) {
        echo $key. " => " . $value . "<br/>";
        if ($key == 4) {
            echo "Loop is paused.<br/>";
            break;
        }
     
        if ($key > 4) { # This won't work.
            echo "Loop is continued.<br/>";
            continue;
        }
    }
    
    Code (markup):
    Any ideas how to get it work?
     
    ketting00, Jul 30, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Of course that won't work. If you explain what you're trying to do, exactly, maybe we could offer a solution.
     
    PoPSiCLe, Jul 30, 2015 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    OK,
    Here's the result I want:

    0 => 0
    1 => 1
    2 => 2
    3 => 3
    4 => 4
    Loop is paused.
    Loop continues...
    5 => 5
    6 => 6
    7 => 7
    8 => 8
    ....

    Thank you,
     
    ketting00, Jul 30, 2015 IP
  4. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #4
    Well,
    I'm an idiot.
    This works:
    
    foreach($array as $key => $value) {
        echo $key. " => " . $value . "<br/>";
        if ($key == 4) {
            printf( "Loop breaks. <br/>");
        }
    }
    
    Code (markup):
     
    ketting00, Jul 30, 2015 IP
    freelanceDeveloper likes this.
  5. ctyler125

    ctyler125 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I am glad you got a handle on it!
     
    ctyler125, Jul 31, 2015 IP