Looping over multidimensional array using PHP
It requires a little more thinking while working with loops in multidimensional arrays. Let’s see the situation here.
Say we got a two-dimensional array.
<?php
$students[0]["fname"]="John";
$students[0]["lname"]="Cena";
$students[1]["fname"]="Shawn";
$students[1]["lname"]="Patrick";
$students[1]["fname"]="John";
$students[1]["lname"]="doe";
for($outer=0;$outer<count($students);$outer++):
foreach($students[$outer] as $key => $value):
echo "\$students[$key][$innerkey] = ";
echo $students[$key][$innerkey]." ";
echo"<br>";
endforeach;
echo"<br>";
endfor;
// or we can use foreach as outer loop too.
foreach($students as $key => $innerArray):
foreach($innerArray as $innerkey => $value):
echo "\$students[$outer][$key] = ";
echo $students[$outer][$key];
echo"<br>";
endforeach;
echo"<br>";
endforeach;
?>
This will gives output:
$students[0][fname] = John
$students[0][lname] = Cena
$students[1][fname] = Shawn
$students[1][lname] = Patrick
$students[2][fname] = John
$students[2][lname] = doe
If you have any issue , let us know in the comment box.
Latest
SOLID Principles: A Comprehensive Guide with Examples
Dec 22, 2024
React useEffect, useMemo, and useCallback: A Comprehensive Guide
Nov 23, 2024
React Component Structure, Composition, HOCs and Optimization with Examples
Oct 4, 2023
How to copy text to the clipboard in React.js
Sep 29, 2023
News
Pure html popover API is coming in Chromium 114
Sep 24, 2023
GitHub and OpenAI launch Copilot - an AI pair programmer
Jul 1, 2021
CSS aspect-ratio property
Jan 31, 2021
AWS UI - a collection of React components
Jan 20, 2021
Featured
SOLID Principles: A Comprehensive Guide with Examples
Dec 22, 2024
React useEffect, useMemo, and useCallback: A Comprehensive Guide
Nov 23, 2024
Axios Interceptors in a React application
May 27, 2023
Mastering Laravel: Exploring the Power of implode() and explode() Functions for String Manipulation
May 22, 2023