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 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,248 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,248 Points

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