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 PHP Basics (Retired) PHP Datatypes PHP Datatypes Challenge

Phil White
PLUS
Phil White
Courses Plus Student 9,519 Points

Stuck on PHP Basics question....

Time to add an associative array named $favorite_colors. The keys will be, in order, the following: 'mike', 'jane', & 'chris' with values for each. Those values are 'green', 'blue', & 'yellow'

How do i do this?

3 Answers

It's probably best to rewatch the video to remind yourself.

if you give up, here's the answer:

<?php

$favorite_colors = array  (
'mike' => 'green',
'jane' => 'blue',
'chris' => 'yellow'
)

You can then call each element of the array by their key

<?php

$mike_fav_col = $favorite_colors['mike'];  // == 'green'

All Challenges

<?php

$integer_one = 1;
$integer_two = 2;
$golden =  1.618;
$bool = true;
$colors = array('red','blue','green');
echo $colors[1];
$favorite_colors = array  (
'mike' => 'green',
'jane' => 'blue',
'chris' => 'yellow'
);


?>
Phil White
Phil White
Courses Plus Student 9,519 Points

Before i asked this question i put exactly what you put as the answer and it wouldn't work? Now when i copy and paste your answer it still doesn't work....?

Sorry man, I forgot commas! I updated, try again

Phil White
Phil White
Courses Plus Student 9,519 Points

Tried that as well still wont work? Thank you for your help by the way.

This has worked for me. It sounds like one of your other tests might be failing. Make sure everything has a semi-colon.

I've updated above with the full challenge code.

Phil White
Phil White
Courses Plus Student 9,519 Points

Lol my own stupidity was down to a missing semi-colon, Thanks for your help! Much appreciated!

Ryan Watson
Ryan Watson
5,471 Points

$favorite_colors=array("mike"=>"green", [next key-value pair]);

or repeat this format for each key-value pair:

$favorite_colors['mike']="green";

$favorite_colors['jane']="blue";

Ryan Watson
Ryan Watson
5,471 Points

it may be looking for the second format

Phil White
Phil White
Courses Plus Student 9,519 Points

This also didn't work. Thanks for your help anyway

Ryan Watson
Ryan Watson
5,471 Points

Post your code exactly how you are trying to enter it into the code challenge so we can see if there is a syntax error or something.

Phil White
Phil White
Courses Plus Student 9,519 Points

Don't worry. problem solved thanks for trying to help anyway!

Ladislav Vysmek
Ladislav Vysmek
1,666 Points

I forgot for semicolon, OMG :-D