How To Use Multidimensional Array With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

Multidimensional array is an array of arrays. multidimensional arrays that are two, three, four, five, or more levels deep but hard to manage more than three levels.

Multidimensional_Arrays

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>";
?>

We Can also Use Loop For  Two-dimensional Arrays:
Print all the values of Two-dimensional Arrays through the foreach loop.


Example(2)
<?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>";
}
?>

Complete Code of Multidimensional Arrays:
<!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>


 

Related Post