Nested for loop break ( multi-level up to 4 layer)

Php

Nested for loop break ( multi-level up to 4 layer)

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’t loop anymore

    $array1 = array('1','2','3','4');
    $array2 = array('1','2','3','4');
    $array3 = array('1','2','3','4');
    $array4 = array('1','2','3','4');

    foreach($array1 AS $info1)
    {
        echo "Loop A - $info1 <br>";
        foreach($array2 AS $info2)
        {
            echo "Loop B - $info2 <br>";
            foreach($array3 AS $info3)
            {
                echo "Loop C - $info3 <br>";
                foreach($array4 AS $info4)
                {
                    echo "Loop D - $info4 <br>";
                    if($info4 == rand(1,4))
                    {
                        break 2;
                    }
                }
            }
        }
        echo "<hr>";    
    }
Back To Top