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 trialChristopher Parke
21,978 Pointsphp associative array
not sure what's wrong with my submission. This seems to work in the browser.
<?php
$contacts = array(
array('name'=>'Alena Holligan', 'alena.holligan@teamtreehouse.com'),
array('name'=>'Dave McFarland', 'dave.mcfarland@teamtreehouse.com'),
array('name'=>'Treasure Porth', 'treasure.porth@teamtreehouse.com'),
array('name'=>'Andrew Chalkley', 'andrew.chalkley@teamtreehouse.com'),
);
The error message I'm getting is the always helpful, "Try again!".
Any Idea what she's trying to ask of me? Spent the last few hours trying to decode the question. Btw, there's a typo in the question. Good attn to detail bros.
<?php
//edit this array
$contacts = array(
array('name'=>'Alena Holligan', 'alena.holligan@teamtreehouse.com'),
array('name'=>'Dave McFarland', 'dave.mcfarland@teamtreehouse.com'),
array('name'=>'Treasure Porth', 'treasure.porth@teamtreehouse.com'),
array('name'=>'Andrew Chalkley', 'andrew.chalkley@teamtreehouse.com'),
);
/*$contacts = array(
array('name'=>'Alena Holligan', 'email'=> 'alena.holligan@teamtreehouse.com'),
array('name'=>'Dave McFarland', 'email'=> 'dave.mcfarland@teamtreehouse.com'),
array('name'=>'Treasure Porth', 'email'=> 'treasure.porth@teamtreehouse.com'),
array('name'=>'Andrew Chalkley', 'email' => 'andrew.chalkley@teamtreehouse.com'),
);*/
echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";
echo "<li>Dave McFarland : dave.mcfarland@teamtreehouse.com</li>\n";
echo "<li>Treasure Porth : treasure.porth@teamtreehouse.com</li>\n";
echo "<li>Andrew Chalkley : andrew.chalkley@teamtreehouse.com</li>\n";
echo "</ul>\n";
3 Answers
Sergey Podgornyy
20,660 PointsFirst you need to create subarrays for each user:
<?php
$contacts = array(
array('name' => 'Alena Holligan'),
array('name' => 'Dave McFarland'),
array('name' => 'Treasure Porth'),
array('name' => 'Andrew Chalkley'),
);
// . . .
Then add emails for each users:
<?php
$contacts = array(
array('name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com'),
array('name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com'),
array('name' => 'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com'),
array('name' => 'Andrew Chalkley', 'email' => 'andrew.chalkley@teamtreehouse.com'),
);
// . . .
And after that replace hardcoded text with data from array:
<?php
// . . .
echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>", $contacts[0]['name'], " : ", $contacts[0]['email'], "</li>\n";
echo "<li>", $contacts[1]['name'], " : ", $contacts[1]['email'], "</li>\n";
echo "<li>", $contacts[2]['name'], " : ", $contacts[2]['email'], "</li>\n";
echo "<li>", $contacts[3]['name'], " : ", $contacts[3]['email'], "</li>\n";
echo "</ul>\n";
Christopher Parke
21,978 PointsError message is "Bummer Try again!"
<?php
$contacts = array(
array('name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com'),
array('name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com'),
array('name' => 'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com'),
array('name' => 'Andrew Chalkley', 'email' => 'andrew.chalkley@teamtreehouse.com'),
);
echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>", $contacts[0]['name'], " : ", $contacts[0]['email'], "</li>\n";
echo "<li>", $contacts[1]['name'], " : ", $contacts[1]['email'], "</li>\n";
echo "<li>", $contacts[2]['name'], " : ", $contacts[2]['email'], "</li>\n";
echo "<li>", $contacts[3]['name'], " : ", $contacts[3]['email'], "</li>\n";
echo "</ul>\n";
Sergey Podgornyy
20,660 PointsFollow by steps, that I provided to you. Do not copy all code for the task one. First of all, modify $contacts
array, then modify it one more time, and only after that change static list item
s
Sergey Podgornyy
20,660 PointsYou are welcome
Christopher Parke
21,978 PointsChristopher Parke
21,978 PointsI copied/pasted your code and it's giving me "Bummer, try again!". These are the instructions:
"In the code below, we have a simple array of contact names. We want to use the $contacts array to fill in the hardcoded list of names an email addresses. To hold the email as well as the name for each contact, we need a multidimensional array, meaning each person in the contact list will have their own array. The first step is to make each person their own single item ASSOCIATIVE array, using 'name' as the key for these internal arrays."
Sergey Podgornyy
20,660 PointsSergey Podgornyy
20,660 PointsFor the first part you need just subarrays only with keys
name
: