How Can I Loop Through Multidimensional Array To Display The Table Below?

I have an assignment and it states that I need to use a foreach loop to loop through the below array and print the code in the format below. I have Googled this problem a couple of different ways and found nothing that addressed on how to use an associative array that is multidimensional with the same keys. Can someone help me to create the foreach loop to display the data correctly by creating the correct foreach loop to do that? And how can I sort the array in shows the titles of the movies alphabetically in ascending order?

<?php
$items = array(
     array("title" => "Guardians of the Galaxy Vol. 2", 'type' => "blue-ray","price" => 19.99),
	 array("title" => "Wonder Woman 2017", 'type' => "4K","price" => 24.99),
	 array("title" => "Spider-Man: Homecoming", 'type' => "blue-ray","price" => 22.99),
	 array("title" => "War For The Planet Of The Apes", 'type' => "4K","price" => 19.99),
	 array("title" => "Baby Driver", 'type' => "blue-ray","price" => 24.99),
	 array("title" => "Atomic Blonde", 'type' => "4K","price" => 24.99),
	 array("title" => "Moana", 'type' => "blue-ray","price" => 15.99),
	 array("title" => "Alien: Covenant", 'type' => "blue-ray","price" => 21.96),
	 array("title" => "Despicable Me 3", 'type' => "4K","price" => 24.99),
	 array("title" => "Firefly Complete Series", 'type' => "blue-ray","price" => 20.99),
);
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Available Movie Titles</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
<style>
body {
	margin-top:50px;
	overflow:scroll;
}
</style>

</head>

<body>

<div class="row">

  	<div class="col-md-8 col-md-offset-2">
      <table class="table table-striped table-hover">
              <thead>
                <tr>
                  <th>Title</th>
                  <th>Blue Ray</th>
                  <th>4K</th>
                  <th>Price</th>
                </tr>
              </thead>
              <tbody>
              	
              	<!--start of row that you need to include for each item in the array-->
                <tr>
                  <td>title goes here</td>
                  <td>if type is equal to blue-ray, then show a yes</td>
                  <td>if type is equal to 4K, then show a yes</td>
                  <td>price goes here</td>
                </tr>
                <!--end of row that you need to include for each item in the array-->
                
              </tbody>
            </table>
    </div>
    
</div> 

</body>

</html>
foreach ($arr as $key => $value) {
 echo $key;
}

http://php.net/manual/en/array.sorting.php

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.