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 trialALBHAISI MOHAMMED
1,564 Pointsuse the array
Required : Replace the hard coded values in the output with the name and email values from the contacts array. Error that I get : Use the contacts array to output the name of each contact.
<?php
$contacts[] = [
'name' => 'Alena Holligan',
'email' => 'alena.holligan@teamtreehouse.com',
'name' => 'Dave McFarland',
'email' => 'dave.mcfarland@teamtreehouse.com',
'name' => 'Treasure Porth',
'email' => 'treasure.porth@teamtreehouse.com',
'name' => 'Andrew Chalkley',
'email' => 'andrew.chalkley@teamtreehouse.com'
];
$contacts[0]['name'];
$contacts[0]['email'];
?>
2 Answers
Sean T. Unwin
28,690 PointsThe following is two examples for creating the associative array:
<?php
$contacts = [
[
'name' => 'Alena Holligan',
'email' => 'alena.holligan@teamtreehouse.com'
],
[
'name' => 'Dave McFarland',
'email' => 'dave.mcfarland@teamtreehouse.com'
],
//....
];
// OR
$contacts[] = [
'name' => 'Alena Holligan',
'email' => 'alena.holligan@teamtreehouse.com'
];
$contacts[] = [
'name' => 'Dave McFarland',
'email' => 'dave.mcfarland@teamtreehouse.com'
];
The following is an example for echo
'ing out the names and emails:
<?php
echo "<li>" . $contacts[0]['name'] . " : " . $contacts[0]['email'] . "</li>\n";
For each of the others in the array, replace the 0
with the appropriate index number.
richporter
16,727 PointsFew things!
- $contacts[] appends to the array instead of assigning an array to $contacts (like a variable).
2 .Your name and email assignments are fine for each person, but each person is not inside of their own array.
- $contacts[0]['name'] & $contacts[0]['email'] are correct, but you need to insert those for each person into the output string that was already provided.
ALBHAISI MOHAMMED
1,564 PointsI am stuck. Couldn't figure it out honestly. I don't know what is wrong exactly. I've tried the below :
<?php
$contacts[] = [
'name' => 'Alena Holligan',
'email' => 'alena.holligan@teamtreehouse.com',
];
$contacts[] = [
'name' => 'Dave McFarland',
'email' => 'dave.mcfarland@teamtreehouse.com',
];
$contacts[] = [
'name' => 'Treasure Porth',
'email' => 'treasure.porth@teamtreehouse.com',
];
$contacts[] = [
'name' => 'Andrew Chalkley',
'email' => 'andrew.chalkley@teamtreehouse.com',
];
$contacts[0]['name'];
$contacts[0]['email'];
?>
Error : Use the contacts array to output the name of each contact.
richporter
16,727 PointsYou haven't done anything that task #3 is asking, "Replace the hard coded values in the output with the name and email values from the contacts array."
Try and figure out the rest from what is below.
<?php
echo "<ul>\n";
echo "<li>".$contacts[0]['name']." : ".$contacts[0]['email']."</li>\n";
// second person here..
// third person here..
// fourth person here..
echo "</ul>\n";