Break does not work on foreach

Asked

Viewed 159 times

2

i have a foreach inside another foreach that will run for various values of a specific object. But I need to stop the two foreach’s when it goes inside the if, however the command break does not work for some reason, it just keeps running the two foreach’s. The code is more or less like this:

foreach($conteudo["data"] as $oddss) {

    foreach($oddss["types"]["data"] as $oddss2) {
        if($oddss2["type"] == "1x2") {

            $bookmarker = $oddss["bookmaker_id"];

            foreach($oddss2["odds"]["data"] as $oli) {

                switch($oli["label"]) {
                    case 1:
                        $cotCasa = $oli["value"];
                        break;
                    case 2:
                        $cotFora = $oli["value"];
                        break;
                    case "X":
                        $cotEmp = $oli["value"];
                }

            }
            break;
        }
    }

}
  • 2

    I don’t quite understand, is break where the switch. In the } break;} place break 2; see if this is the desired result.

  • That’s exactly what I needed. I didn’t know there was such a break 2. Thank you!

  • It can be any number just can’t be a variable :)

  • All right, thanks a lot!!

1 answer

2


The break is coming out of switch and not of foreach, change him to if/elseif who doesn’t need break to get out and then the break will leave the foreach. Or use the break 2 to make two exits, one of the switch and another of foreach.

  • Thank you, that’s what I needed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.