PHP foreach with Nested Array?
To loop through a nested array in PHP using a foreach loop, you can use nested foreach loops.
To loop through a nested array in PHP using a foreach loop, you can use nested foreach loops. The outer loop iterates through the top-level array, and the inner loop iterates through the sub-arrays. Here is an example:
Example of using nested foreach loops in PHP
php— editable, runs on the server
Info
Watch a video course: Learn object oriented PHP
This will output:
1
2
3
4
5
6
7
8
9You can use nested foreach to loop over more than 2 level nested arrays as well.