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 trialVladislav Mavrin
Courses Plus Student 3,125 PointsEchoing php array in html <li></li> tags
Got stacked on the 3d step. I assume there is something wrong with the brackets when trying to echo array in a html tag.
Would appreciate your help, thanx!
<?php
//edit this array
$contact1 = array('name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com');
$contact2 = array('name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com');
$contact3 = array('name' => 'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com');
$contact4 = array('name' => 'Andrew Chalkley', 'email' => 'andrew.chalkley@teamtreehouse.com');
$contacts = array($contact1,$contact2,$contact3,$contact4);
echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>";
echo $contacts['name']['email'];
echo "</li>\n";
echo "</ul>\n";
2 Answers
Corey Cramer
9,453 PointsYou are very close. I have a sample below to help you.
<?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"; // Duplicate this incrementing by one for each contact
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";
Vladislav Mavrin
Courses Plus Student 3,125 PointsThank you Corey!
so {} : {} this is used to bring together several values of a massive?
Corey Cramer
9,453 PointsI believe you can actually use variables inside of strings so long as you use the double quotation marks with the curly brackets being optional. I use them because it helps make sure that the variables I am including in the string more apparent.
This particular example isn't a good illustration of why I personally think it's important because accessing the array indexes and keys makes it stand out but if it was just $name and $email tucked into there I'm prone to missing it.
Vladislav Mavrin
Courses Plus Student 3,125 PointsVladislav Mavrin
Courses Plus Student 3,125 PointsHey Corey, any idea why this is not accepted by as the right answer. The output seems to be OK to me
Corey Cramer
9,453 PointsCorey Cramer
9,453 PointsVladislav,
I believe this is a matter of the tests looking for a certain string or substring(s) in your code to verify that you have it right and not looking at the output. Your solution is significantly better because the same 3 lines (your foreach) would be an adequate solution no matter how large the contacts list grew.