![]() |
Multidimensional array in PHP |
Multidimensional array-
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array or further contain array within itself and so on. Values in the multi-dimensional array are access using multiple index.
Example-
In this example we create a two dimensional array to store information of three person in three category (Name, Email, Mobile).
In this example used an associative array, you can use the numeric array in the same term.
<!DOCTYPE html>
<html>
<head>
<title>2D Array</title>
</head>
<body>
<?php
//Create array
$info = array(
'Dharmveer' => array(
"Name" => "Dharmveer",
"Email" => "dharmveer@gmail.com",
"Mobile" => "0123456789"
),
"Monu" => array(
"Name" => "Monu",
"Email" => "monu@gmail.com",
"Mobile" => "0987654321"
),
"Mohit" => array(
"Name" => "Mohit",
"Email" => "mohit@gmail.com",
"Mobile" => "1234554321"
)
);
//Accessing array
echo "Email of Mohit : ";
echo $info['Mohit']['Email']."<br>";
echo "Mobile of Monu : ".$info['Monu']['Mobile']."<br>";
echo "Email of Monu : ".$info['Monu']['Email'];
?>
</body>
</html>
|
Output-
Example-
In this example we create a two dimensional array to store information of three person in three category (Name, Email, Mobile).
In this example used an associative array, you can use the numeric array in the same term.
And in this example accessing 2D array using foreach loop.
<!DOCTYPE html>
<html>
<head>
<title>2D Array</title>
</head>
<body>
<?php
//Create array
$info = array(
'0' => array(
"Name" => "Dharmveer",
"Email" => "dharmveer@gmail.com",
"Mobile" => "0123456789"
),
"1" => array(
"Name" => "Monu",
"Email" => "monu@gmail.com",
"Mobile" => "0987654321"
),
"2" => array(
"Name" => "Mohit",
"Email" => "mohit@gmail.com",
"Mobile" => "1234554321"
)
);
//Accessing array using loop
foreach ($info as $key => $value) {
echo $value['Name']."<br>".$value['Email']."<br>".$value['Mobile']."<br><br>";
}
?>
</body>
</html>
|
Output-
0 Comments