Foreach loops on multidimensional arrays in PHP
Inspired by https://bit.ly/3psMXqD
If you have a multidimensional array you don’t have to proceed it like this:
<?php
    $array = [
        ["AA", "AB", 
            ["ACA", "ACD"]
        ],
        ["BA", "BB", 
            ["BCA", "BCD"]
        ],
        ["CA", "CB", 
            ["CCA", "CCD"]
        ],
    ];
    foreach($array as $a){
        foreach($a as $b){
            if(is_array($b)){
                foreach($b as $c){
                    echo "$c ";
                }
            }else{
                echo "$b ";
            }
        }
    }
?>
You can simply make it like this:
<?php
    $array = [
        ["AA", "AB", 
            ["ACA", "ACD"]
        ],
        ["BA", "BB", 
            ["BCA", "BCD"]
        ],
        ["CA", "CB", 
            ["CCA", "CCD"]
        ],
    ];
    foreach($array as list($a, $b, list($c, $d))) {
        echo "$a $b $c $d ";
    };
?>
For every new dimension you just need to add a new dimension of the list() function.
    Written on March  9, 2022
  
