How Can I Loop Through A Multidimensional Array And Put In Table


I am doing a homework assignment and am having trouble finding out how to code the problem. I have performed multiple searches on Google but found nothing on how to use a foreach loop using the same keys with multiple values in a multidimensional array.

I have two questions:

How can I figure out how to do two use a foreach loop and loop through the arrays in this code and print out the values to test the foreach loop?

How can I print out the code that is in the html below the php code within the <td> tags.

Here’s a foreach loop that I thought would work but it doesn’t print out the code correctly to test values.

foreach ($items as $title=>$value) {`
echo $items." – ".$value."<br />";

$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>
<meta charset="utf-8">
<title>Available Movie Titles</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
body {
<div class="col-md-8 col-md-offset-2">
  <table class="table table-striped table-hover">
              <th>Blue Ray</th>
          	<!--start of row that you need to include for each item in the array-->
              <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>
            <!--end of row that you need to include for each item in the array-->

$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)

echo "<table><tr><th>Title</th><th>Type</th><th>Price</th></tr>";
foreach($items as $key=>$details) {
    echo "<tr><td>" . $details["title"] . "</td><td>" . $details["type"] . "</td><td>" . $details["price"] . "</tr>";
echo "</table>";


Thank you @ErnieAlex!


You are welcome… You would most likely want to use some CSS to make it pretty… LOL Good luck!


When I print the results of this loop on the screen in the table, I would like to extend the subheader ‘Price’ under the main header ‘Price’, and also to compare the types of DVDs to find out if they are Blu-Ray or 4K and to display which category the type of DVD was under (Blu-ray or 4K). I would also, of course like to extend the price of the DVD under the price column.

Please tell me if I am correct in this, do I need to use if else statements within the loop to put the types of DVDs under the correct categories? Please also tell me if I am correct when I say that should I include the if else statements within the foreach loop?

If not, if else statements, what should I use? And any suggestions or questions that would lead me what to use and where to use it?

And are there any suggestions/questions to help me figure out how to extend the table rows so that the date within the

tags would all be under the correct categories?


Well, for me, I would add more types to your list. I mean like DVD, Blu-Ray, Blu-Ray 4k, etc…
Then, you don’t have to deal with IF clauses. Most programming issues are best solved before you write the code by being logical about the inputs and designing them before you get the data into your database. Well, that is my humble opinion.

Now, with that said, you could create an additional field to tell if it is 4K or not, and another for 2180 vs 1080 P versions, but, I think since there is not very many different types, just add the Blu-Ray-4K into the group.

Extended the price? Do you mean have one title and several different prices for each type? Since you have a header (title) of PRICE, you can place all of the prices in one cell of the table if you wanted to. But, this would require a bit more programming as you would need to group the titles and handle them in a different way. Not really sure what you are asking there. If you mean you want them sorted by title, that is easy to do as you can sort an array on any one column. Hmmm, more info needed I guess…


Thank you @ErnieAlex for answering so quickly!

Here’s the results that I am looking for on my assignment help:



You are a very good php coder. I want to get as good as you. Are there any books that helped you? Did you have a mentor? I might need a mentor or something because these php assignments are just so challenging to me!

Yes, it was hard to describe all of this at the beginning of this post. When I told you at the beginning that I needed help with my homework, I should have included all of this at the beginning and gave this assignment more context :slight_smile:


Thank you. I am not that good, but, have been programming since it started. i am just trying to give some back and help others. It is an enjoyable process. As far as a mentor goes, there is a site that covers just about everything in programming. Here is a link to it. I set the link to the discussion on PHP compares or operators. From there, you can select HTML, CSS or PHP on the top as needed to learn things. Then, on the left, you can select various areas of programming and learn from there. It is not always the best code on this site, but, it is a great start. Hope it helps going forward. w3schools

So, I do not want to do all your homework for you, but, can steer you in the right direction. First, a table is just a series of rows. Each row has a “cell” or table detail area. In there you display on the screen whatever data you want. You can use a compare and either display a value or a blank and that would work just fine for your use. But, in your instructions, it says to use the correct type of coding. Since I am not taking your course, I have no idea what you have learned so far. Have you learn about ternary compares yet? They would make the job easy to change.

To give you more hints, first you would need to change the title row of your table. (That is where the TH’s are.) The one that I had created for TYPE would be needed to be split into two. One for 4K and one for Blue-Ray. Remember that you need the same number of titles as you do details in each row. So, the displaying of the data will be needed to be changed. Then, when you split the echo to have two TD’s for the type display, And, then add in a compare to display the correct YES or NO depending on the data.

Have you learned about ternary compares yet?


No, not yet. I have learned a little about about just ternary operators in my previous Intro to C++ Programming Class.

Thanks for the tips!


This is where Classes come in handy. It makes things easier to do.


These are online classes. Maybe I need to get a coding tutor because access to the instructors are fairly limited on campus because of my work schedule and their office hours. And they just give slight hints when I ask questions about my programs.


I was actually referring to a data structure class, that would model what you are attempting to display. I’m free to answer questions usually.


LOL I didn’t think he was up to that yet… Funny!


I didn’t think so either, but each response, I get more knowledgeable about where he is an what he knows. Considering he is also doing C++, it is the same thing.


Oh, okay. Thanks for being open to questions from me. What is the best way to learn how to code for the web, being that I want to become a Web Developer in the future. I am currently pursuing an AS in Computer Programming & Web Development. I am unable to focus on just web development because the nature of the degree.


Well, that one opens a huge door… Every programmer likes certain programs and ways to handle developing sites. Programming IS what web development really is! Most sites in the world contain tons of programming behind the scenes.

Anyway, just going from what is said on sites like these, you need two things to get started. A server to store your experiments (test sites) on and a good solid IDE to let you do the programming in. A lot of people here use NetBeans for the IDE. If you have a formal server you can attach it directly to the server and update your files with ease that way. Or, if you want to have a local server set up to test on your computer, you can use WAMP for a PC and MAMP for a Mac. These are server emulators. They are free to download and use. You can install it with PHP, MySQL and other libraries, so you basically turn your computer into a server system. Then, save your site on your computer and test offline. Since you are already in school chasing an “AS”, then you are on the right track. And, setting up a system for testing would be the next step.

Netbeans can be downloaded for free here: Netbeans Downloads
( I use the PHP version. You might want the ALL version if you are working in C++. )

Wamp here: Wamp Server
Mamp here: Mamp Server

Once these are installed, you can bring up the MySQL’s control panel and set up a database on your local computer. Then, create a site and run tests as needed. Also, you can download free website templates and alter them so you can learn how to do the various 8 or 10 languages that are used on various websites.
And, lastly, of course, come back here when you get stuck… Good luck!

Oh, also, Sean, a class is a way to handle OOP. A class creates an object which is made up of data and processes that pertain to that class. So, like a class for address might have data like street, city, state and processes like enter, update, delete, locate-in-world. It is a way to organize parts of your system. You should be learning about OOP and Classes in your study class. (Not a “PHP code class”, hence the funny part… )


I appreciate both of you guys’ help.

I’ve heard of this site from and I am going to sign up for the free Intro to Computer Science course and take it most likely over my winter semester break. I think it would be very helpful for me to take. You can take all kinds of different and free courses through it.