1. Two-dimensional Arrays.
A two-dimensional array is an array of arrays.
Example(1)
<?php
$bikes = array (
array("Bike-1",35,'43,174'),
array("Bike-2",40,'49,174 '),
array("Bike-3",42,'53,174'),
array("Bike-4",17,' 70,174')
);
echo "<h4> Model ". $bikes[0][0].": Mileage: ".$bikes[0][1]."kmpl, cost: ".$bikes[0][2].".</h4>";
echo "<h4> Model ". $bikes[1][0].": Mileage: ".$bikes[1][1]."kmpl, cost: ".$bikes[1][2].".</h4>";
echo "<h4> Model ". $bikes[2][0].": Mileage: ".$bikes[2][1]."kmpl, cost: ".$bikes[2][2].".</h4>";
echo "<h4> Model ". $bikes[3][0].": Mileage: ".$bikes[3][1]."kmpl, cost: ".$bikes[3][2].".</h4>";
?>
foreach
loop.
<?php
$bikes = array(
array("Bike-1", '35 kmpl', 'Rs. 43,174 '),
array("Bike-2", '40 kmpl', 'Rs. 49,174 '),
array("Bike-3", '42 kmpl', 'Rs. 53,174 '),
array("Bike-4", '17 kmpl', 'Rs. 70,174 ')
);
foreach ($bikes as $mark) {
echo "<br>";
echo "<h4>" . $mark[0] . "</h4>";
echo "<h4>" . $mark[1] . "</h4>";
echo "<h4>" . $mark[2] . "</h4>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Multidimensional Arrays</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="text-center">
<h1>PHP Multidimensional Arrays
</h1>
</div>
<h2> Two-dimensional Arrays.</h2>
<h2>1.Creating Two-dimensional Arrays Without Loop</h2>
<div class="well">
<?php
$bikes = array (
array("Bike-1",35,'43,174'),
array("Bike-2",40,'49,174 '),
array("Bike-3",42,'53,174'),
array("Bike-4",17,' 70,174')
);
echo "<h4> Model ". $bikes[0][0].": Mileage: ".$bikes[0][1]."kmpl, cost: ".$bikes[0][2].".</h4>";
echo "<h4> Model ". $bikes[1][0].": Mileage: ".$bikes[1][1]."kmpl, cost: ".$bikes[1][2].".</h4>";
echo "<h4> Model ". $bikes[2][0].": Mileage: ".$bikes[2][1]."kmpl, cost: ".$bikes[2][2].".</h4>";
echo "<h4> Model ". $bikes[3][0].": Mileage: ".$bikes[3][1]."kmpl, cost: ".$bikes[3][2].".</h4>";
?>
</div>
<br>
<h2>2.Creating Two-dimensional Arrays With Loop</h2>
<div class="well">
<?php
$bikes = array(
array("Bike-1", '35 kmpl', 'Rs. 43,174 '),
array("Bike-2", '40 kmpl', 'Rs. 49,174 '),
array("Bike-3", '42 kmpl', 'Rs. 53,174 '),
array("Bike-4", '17 kmpl', 'Rs. 70,174 ')
);
foreach ($bikes as $mark) {
echo "<br>";
echo "<h4>" . $mark[0] . "</h4>";
echo "<h4>" . $mark[1] . "</h4>";
echo "<h4>" . $mark[2] . "</h4>";
}
?>
</div>
</div>
</body>
</html>