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 trialPhil White
Courses Plus Student 9,519 PointsStuck 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
thomascawthorn
22,986 PointsIt'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'
);
?>
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
5,471 Pointsit may be looking for the second format
Phil White
Courses Plus Student 9,519 PointsThis also didn't work. Thanks for your help anyway
Ryan Watson
5,471 PointsPost 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
Courses Plus Student 9,519 PointsDon't worry. problem solved thanks for trying to help anyway!
Ladislav Vysmek
1,666 PointsI forgot for semicolon, OMG :-D
Phil White
Courses Plus Student 9,519 PointsPhil White
Courses Plus Student 9,519 PointsBefore 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....?
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsSorry man, I forgot commas! I updated, try again
Phil White
Courses Plus Student 9,519 PointsPhil White
Courses Plus Student 9,519 PointsTried that as well still wont work? Thank you for your help by the way.
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsThis 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
Courses Plus Student 9,519 PointsPhil White
Courses Plus Student 9,519 PointsLol my own stupidity was down to a missing semi-colon, Thanks for your help! Much appreciated!