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 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