Foreach loop through multidimensional array in PHP
In this post we will give you information about Foreach loop through multidimensional array in PHP. Hear we will give you detail about Foreach loop through multidimensional array in PHPAnd how to use it also give you demo for it if it is necessary.
Use the PHP nested loop
You can simply use the foreach
loop in combination with the for
loop to access and retrieve all the keys, elements or values inside a multidimensional array in PHP.
Let’s take a look at the following example to understand how it basically works:
<?php
// Multidimensional array
$superheroes = array(
"spider-man" => array(
"name" => "Peter Parker",
"email" => "[email protected]",
),
"super-man" => array(
"name" => "Clark Kent",
"email" => "[email protected]",
),
"iron-man" => array(
"name" => "Harry Potter",
"email" => "[email protected]",
)
);
// Printing all the keys and values one by one
$keys = array_keys($superheroes);
for($i = 0; $i < count($superheroes); $i++) {
echo $keys[$i] . "{<br>";
foreach($superheroes[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
?>
Hope this code and post will helped you for implement Foreach loop through multidimensional array in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs