Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Daniel Stuntz
Daniel Stuntz
1,683 Points

Stuck on "Build a Basic PHP Website" -> "Associative Arrays", Challenge Task 2 of 7

I am stuck on "Build a Basic PHP Website" -> "Associative Arrays", Challenge Task 2 of 7 (link: https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/associative-arrays-2).

I'm not sure whether it's due to a bug / tech. issue, or whether I've gotten "off track" and/or am not properly understanding the objective. However, I keep getting that question wrong. The feedback is "Bummer! Did you assign a value to the "title" key?"

The objective is stated as "Add an element to that array with a key of 'title' and a value of 'The Empire Strikes Back' ".

So, I figured that the answer to be: $movie[] = ["title" => "The Empire Strikes Back"];
(inside the php tag, along with what was added in Challenge Task 1).

I assume that " ["title" => "The Empire Strikes Back"] " is basically a nested array of attributes, with only the first one specified so far), where each element (of that nested array) consists of a key (attribute name, "title" in this case), paired with it's value "The Empire Strikes Back" . Is that correct?

Anyway, I played around some, and tried answering as shown below:

$movie[]["title"] = "The Empire Strikes Back";

But that didn't work, either. I re-read the implied suggestion in "Bummer! Did you assign a value to the 'title' key?"

I apparently thought that the key would paired-up with a value, within the " ["title" => "The Empire Strikes Back"] " expression. I figured that this creates and adds a new element to the $movie array, with the "title" attribute set to "The Empire Strikes Back".

On additional question: Am I supposed to reference the $catalog[k] variable, with some number filled-in for 'k'?

I'd greatly appreciate some feedback, regarding why I'm not getting that challenge task correct.

Thanks.

index.php
<?php 
  $movie = array();
  $movie[]["title"] = "The Empire Strikes Back";
 ?>
<h1>Back to the Future (1985)</h1>

<table>
  <tr>
    <th>Director</th>
    <td>Robert Zemeckis</td>
  </tr>
  <tr>
    <th>IMDB Rating</th>
    <td>8.5</td>
  </tr>
  <tr>
    <th>IMDB Ranking</th>
    <td>53</td>
  </tr>
</table>
Simon Coates
Simon Coates
28,693 Points

if you want to experiment, you can go to http://sandbox.onlinephpfunctions.com/

<?php
$move = []; //this line is optional
$movie[]["title"] = "The Empire Strikes Back";
echo "<pre>";
var_dump($movie);
echo "</pre>";

your original code was producing an array inside an array. Running the above, can help you get a view on what the code does.

2 Answers

Simon Coates
Simon Coates
28,693 Points
<?php 
  $movie = array();
  $movie[]["title"] = "The Empire Strikes Back"; // [] is to add to the end of an array with numbers as keys
 ?>

should be

<?php 
  $movie = array();
  $movie["title"] = "The Empire Strikes Back"; // direct set on key
 ?>

Thank you!! I was making a similar mistake. I don't remember Treehouse going over this direct set on key format, thanks for clearing that up.

Simon Coates
Simon Coates
28,693 Points

As you know the values you initially want in the array, you can also do

$movie = array(
"title"=> "The Empire Strikes Back"
);

or

 $movie = [
"title"=> "The Empire Strikes Back"
];