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 Arrays and Control Structures PHP Arrays Multidimensional Arrays

Vladislav Mavrin
PLUS
Vladislav Mavrin
Courses Plus Student 3,125 Points

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

index.php
<?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

You 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
Vladislav Mavrin
Courses Plus Student 3,125 Points

Hey Corey, any idea why this is not accepted by as the right answer. The output seems to be OK to me

<?php
//edit this array
$contacts = array();
  $contacts[] = array ('name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com');
  $contacts[] = array ('name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com');
  $contacts[] = array ('name' => 'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com');
  $contacts[] = array ('name' => 'Andrew Chalkley','email' => 'andrew.chalkley@teamtreehouse.com');


foreach ($contacts as $contact) {
  echo "<ul>\n<li>" . $contact['name'] . " : " . $contact['email'] . "</li></ul>\n";
}
ยดยดยด

Vladislav,

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.

Vladislav Mavrin
PLUS
Vladislav Mavrin
Courses Plus Student 3,125 Points

Thank you Corey!

so {} : {} this is used to bring together several values of a massive?

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