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 Object-Oriented PHP Basics Building the Recipe Accessing Arrays

Accessing arrays: failing task despite echoing correct variable.

echo $favorite_colors['mike']; returns green, which is asked for by the task.

I have tried changing around the order of the $colors array variables, and the associative array indexes, but nothing will allow me to pass this task.

Anybody else have any luck?

I know I have gotten it correct, it's just bugging me that I can't pass the task.

index.php
<?php

//Place your code below this comment
$colors = array(
  'red',
  'blue',
  'green'
);

$favorite_colors = array(
    'mike' => $colors[2],
  'jane' => $colors[1],
  'chris' => $colors[0]
);

echo $favorite_colors['mike']; // green
?>

1 Answer

jamesjones21
jamesjones21
9,260 Points

I just did the challenge again to get my head around it:

the following code passed:

/Place your code below this comment
$colors = ['red','blue','green'];

echo $colors[1];

$favorite_colors = [
  'mike' => $colors[2],
  'jane' => $colors[1],
  'chris' => $colors[0]
  ];

echo $favortie_colors['mike'];

But out of curiosity what error message comes up? Your output is all correct, too.

No way.

As for the error message, I was getting a 'looks like you are echoing out the wrong thing' error, which was incorrect.

It turns out you must echo out both the previous task 'blue' and the final task 'green' in order for it to pass. Therefore adding both:

echo $colors[1]; // blue
echo $favorite_colors['mike']; // green

Passes the task. I removed the previous task echo as I thought it was no longer needed. Frustrating!

Thanks for the help.

jamesjones21
jamesjones21
9,260 Points

Some of the challenges do need to be reviewed in my eyes, some require to be re-worded too.