php foreach multidimensional array Example

Today, We want to share with you php foreach multidimensional array.In this post we will show you foreach loop in php, hear for how to print multidimensional array in php using for loop? we will give you demo and example for implement.In this post, we will learn about how to print multidimensional array in php? with an example.

Foreach loop through multidimensional array in PHP

Use the PHP nested loop

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of Looping Over PHP Multidimensional Array</title>
</head>
<body>

<?php
// Multidimensional array
$members_list = array(
    "spider-man" => array(
        "name" => "infinityknow Online",
        "email" => "[email protected]",
    ),
    "super-man" => array(
        "name" => "4cgandhi Hindi Font",
        "email" => "[email protected]",
    ),
    "iron-man" => array(
        "name" => "pakainfo Webinfo",
        "email" => "[email protected]",
    )
);
 
// Printing all the keys and values one by one
$keys = array_keys($members_list);
for($i = 0; $i < count($members_list); $i++) {
    echo $keys[$i] . "{<br>";
    foreach($members_list[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "}<br>";
}
?>

</body>
</html>


PHP foreach object properties

Example(index.php)

<?php
 
class Member
{
    public $name;
    public $description;
 
    public function __construct($name, $description) {
        $this->name = $name;
        $this->description = $description;
    }
}
 
$member = new Member('Virat Kohali', 'gardener');
 
foreach ($member as $propName => $propValue) {
    echo "$propName: $propValue\n";
}

php foreach with multidimensional array

Array
(
    [0] => Array
        (
            [id] => 1
            [main_member_title] => Radhika Patel
            [profilenm] => Chovatiya
        )

    [1] => Array
        (
            [id] => 2
            [main_member_title] => Minaxi Kilari
            [profilenm] => Dhameliya
        )

    [2] => Array
        (
            [id] => 3
            [main_member_title] => Bhoomi dave
            [profilenm] => Bhoomi dave
        )
)

index.php

foreach ($rows as $row) {
    echo $row['id'];
    echo $row['main_member_title'];
    echo $row['profilenm'];
}

I hope you get an idea about php foreach associative array.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.