What are some potential pitfalls when using a nested array with a while loop in PHP?

One potential pitfall when using a nested array with a while loop in PHP is not properly resetting the inner array pointer after iterating through it. This can lead to unexpected behavior or infinite loops. To solve this issue, you can use the reset() function to reset the array pointer back to the beginning of the inner array before starting the next iteration.

$nestedArray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

$outerIndex = 0;

while ($outerIndex < count($nestedArray)) {
    $innerIndex = 0;
    
    while ($innerIndex < count($nestedArray[$outerIndex])) {
        echo $nestedArray[$outerIndex][$innerIndex] . " ";
        $innerIndex++;
    }
    
    echo "\n";
    
    reset($nestedArray[$outerIndex]); // Reset inner array pointer
    $outerIndex++;
}