Open In App

How to Use Foreach Loop with Multidimensional Arrays in PHP?

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Multi-dimensional array, the task is to loop through array in PHP. The foreach loop is a convenient way to iterate over arrays in PHP, including multidimensional arrays. When dealing with multidimensional arrays, you can use nested foreach loops to access and process each element. In this article, we will explore how to use foreach loops to iterate through multidimensional arrays in PHP.

Basic Syntax of Foreach Loop

The basic syntax of a foreach loop in PHP is as follows:

foreach ($array as $value) {
// Code to be executed for each $value
}

Iterating Through a Multidimensional Array

To iterate through a multidimensional array using foreach loops, you can nest the loops for each level of the array.

Example 1: In this example, we will use a Multidimensional array to iterate array items through foreach loop.

PHP
<?php

$numbers = array(
    array(10, 20, 30, 40, 50),
      array(15, 30, 45, 60, 75),
      array(20, 40, 60, 80, 100),
      array(25, 50, 75, 100, 125),
      array(30, 60, 90, 120, 150)
);  

foreach ($numbers as $number) {
    foreach ($number as $num) {
        echo "$num, ";
    }
      echo "\n";
}

?>

Output
10, 20, 30, 40, 50, 
15, 30, 45, 60, 75, 
20, 40, 60, 80, 100, 
25, 50, 75, 100, 125, 
30, 60, 90, 120, 150, 

Example 2: In this example, we will use Multidimensional associative array to iterate through foreach loop.

PHP
<?php

$students = array(
    array("name" => "Rakesh", "Age" => 25),
    array("name" => "Rajan", "Age" => 26),
    array("name" => "Akash", "Age" => 28)
);  

foreach ($students as $student) {
    foreach ($student as $key => $value) {
        echo "$key => $value\n";
    }
    echo "\n";
}

?>

Output
name => Rakesh
Age => 25

name => Rajan
Age => 26

name => Akash
Age => 28


Previous Article
Next Article

Similar Reads

How to Loop Through an Array using a foreach Loop in PHP?
Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iterating through associative arrays where you need both t
2 min read
How to loop through HTML elements without using forEach() loop in JavaScript ?
In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Using the for loopUsing the While loopUsing the 'for.....of' statementApproach 1: Using the for loopThe HTML elements can be iterated by using the regular JavaScript for loop. The number of el
3 min read
How to use async/await with forEach loop in JavaScript ?
Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to traverse the array with the help of forEach loop.
2 min read
Iterate associative array using foreach loop in PHP
Given two arrays arr1 and arr2 of size n. The task is to iterate both arrays in the foreach loop. Both arrays can combine into a single array using a foreach loop. Array: Arrays in PHP is a type of data structure that allows to storing multiple elements of similar data type under a single variable thereby saving the effort of creating a different v
2 min read
Determine the first and last iteration in a foreach loop in PHP?
Given an array of elements and the task is to determine first and last iteration in foreach loop. There are many ways to solve this problem which are listed below: Method 1: It is the naive method inside foreach loop to find iteration. Use a counter variable and check when the counter value is zero then it is the first iteration and when the counte
3 min read
PHP | foreach Loop
The foreach construct provides the easiest way to iterate the array elements. It works on array and objects both. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. It allocates temporary memory for index iterations which makes the overall system to redundant its
2 min read
How to find the index of foreach loop in PHP ?
The foreach construct provides the easiest way to iterate the array elements. It works on array and objects both. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. It allocates temporary memory for index iterations which makes the overall system to redundant its
3 min read
How to check foreach Loop Key Value in PHP ?
In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such as To loop through a list of simple values.To loop through a list of key-value pairs. Using the foreach loop with simple values: We can use the foreach loop to access and use the elements in an array of simple values, such as strings or numbers.
2 min read
What is the difference between for and Foreach loop in PHP ?
Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the case of associative arrays. A for loop basically con
3 min read
How to Break Foreach Loop in PHP?
Breaking a foreach loop consists of terminating the execution of the loop prematurely based on a certain condition. In this article, we will explore and learn two approaches to break foreach loop in PHP. Below are the approaches to break foreach loop in PHP: Table of Content Using break KeywordUsing goto KeywordUsing break KeywordIn this approach,
2 min read
Article Tags :