How can you efficiently loop through nested arrays in PHP?

When looping through nested arrays in PHP, you can use recursive functions to efficiently iterate through each level of the array. This allows you to handle arrays of any depth without needing to know the exact structure beforehand. By using a recursive approach, you can easily access and manipulate the elements within the nested arrays.

function loopNestedArray($array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            loopNestedArray($value);
        } else {
            // Perform operations on the individual elements here
            echo $value . "\n";
        }
    }
}

$array = [
    'a' => [1, 2, 3],
    'b' => [4, 5, 6],
    'c' => [
        'd' => [7, 8, 9],
        'e' => [10, 11, 12]
    ]
];

loopNestedArray($array);