Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Multidimensional Arrays

Carl Sergile
Carl Sergile
16,570 Points

Got this question wrong and want to know why?

so this was a multiple choice question. I pick it will return an error but I was wrong. Then after I got it wrong I thought the answer might be the last year on there but not sure.

Quiz Question 2 of 8 What year will this block of PHP code display?

<?php

$movies[] = array(

        "title" => "Star Wars",

        "year" => 1977

    );

$movies[] = array(

        "title" => "The Empire Strikes Back",

        "year" => 1980

    );

$movies[] = array(

        "title" => "Return of the Jedi",

        "year" => 1983

    );

echo $movies[1]["year"];

?>

1 Answer

echo $movies[1]["year"];

The code above is looking for the entry in the $movies array at index 1. PHP arrays begin at index 0, so you'll want to look at the second entry in the $movies array.

 $movies[] =

This pushes (or adds) data to the array, placing it at the bottom of the 'stack', with index 0 being the top of the stack. The second time data is pushed onto the $movies array is when the associative array with title "The Empire Strikes Back" is added:

 $movies[] = array(
     "title" => "The Empire Strikes Back",
     "year" => 1980
 );

The question is looking for the year value in that array, so the answer is 1980.

Carl Sergile
Carl Sergile
16,570 Points

Gotcha, Thanks for the intel!