{"id":666,"date":"2022-10-18T09:51:34","date_gmt":"2022-10-18T09:51:34","guid":{"rendered":"https:\/\/info.juliusgoh.life\/?p=666"},"modified":"2022-10-18T09:54:15","modified_gmt":"2022-10-18T09:54:15","slug":"nested-for-loop-break-multi-level-up-to-4-layer","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=666","title":{"rendered":"Nested for loop break ( multi-level up to 4 layer)"},"content":{"rendered":"<p>In below example will have 4 arrays and will be nested.<br \/>\nBelow example will break 2 which means that it will go back to Loop B.<br \/>\nIn order to go back to Loop A , will be break 3.<br \/>\nbreak 4 will stop the everything, which means it won&#8217;t loop anymore<\/p>\n<pre>\r\n    $array1 = array('1','2','3','4');\r\n    $array2 = array('1','2','3','4');\r\n    $array3 = array('1','2','3','4');\r\n    $array4 = array('1','2','3','4');\r\n\r\n    foreach($array1 AS $info1)\r\n    {\r\n        echo \"Loop A - $info1 &lt;br&gt;\";\r\n        foreach($array2 AS $info2)\r\n        {\r\n            echo \"Loop B - $info2 &lt;br&gt;\";\r\n            foreach($array3 AS $info3)\r\n            {\r\n                echo \"Loop C - $info3 &lt;br&gt;\";\r\n                foreach($array4 AS $info4)\r\n                {\r\n                    echo \"Loop D - $info4 &lt;br&gt;\";\r\n                    if($info4 == rand(1,4))\r\n                    {\r\n                        break 2;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        echo \"&lt;hr&gt;\";    \r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In below example will have 4 arrays and will be nested. Below example will break 2 which means that it will go back to Loop B. In order to go back to Loop A , will be break 3. break 4 will stop the everything, which means it won&#8217;t loop anymore $array1 = array(&#8216;1&#8242;,&#8217;2&#8242;,&#8217;3&#8242;,&#8217;4&#8217;); $array2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":660,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/666"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=666"}],"version-history":[{"count":3,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions\/669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/660"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}