In PHP, you can use a `foreach` loop to iterate over elements in a multidimensional array. The `foreach` loop is particularly useful for iterating through arrays without the need for an index variable. Here’s an example of using `foreach` with a multidimensional array:
<?php // Example multidimensional array $students = array( array('name' => 'John', 'age' => 25, 'grade' => 'A'), array('name' => 'Jane', 'age' => 23, 'grade' => 'B'), array('name' => 'Bob', 'age' => 22, 'grade' => 'C') ); // Iterate over the multidimensional array using foreach foreach ($students as $student) { foreach ($student as $key => $value) { echo "$key: $value "; } echo "\n"; } ?>
In this example, `$students` is a multidimensional array where each sub-array represents a student’s information. The outer `foreach` loop iterates over each student, and the inner `foreach` loop iterates over the key-value pairs within each student’s information.
Output:
name: John age: 25 grade: A name: Jane age: 23 grade: B name: Bob age: 22 grade: C
You can adjust the structure based on your specific multidimensional array. The key point is that the outer `foreach` loop iterates over the main array, and the inner `foreach` loop iterates over each sub-array or associative array within the main array.