PHP Array Tutorial: Display Data with Nested Loops
This Snippet is coded by a user inOnline php Compiler. You can see and run this code too.
زبان: PHP
This PHP code demonstrates how to work with a multidimensional array, also known as a two-dimensional array. It stores car names and two numeric values (likely stock and sold counts) in a single array structure.
The script uses two nested loops to iterate through the rows and columns of the array. The outer loop cycles through each car, while the inner loop prints each piece of data as a list item.
- Array definition: The
$carsarray contains subarrays for each car. - Outer loop: Iterates over each row (car) and displays a row number.
- Inner loop: Iterates over each column (attribute) and outputs the value in a bullet list.
This pattern is useful for displaying tabular data in a simple HTML list format. It's a common learning example for beginners to understand loop nesting and array traversal in PHP.