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

Sean Hylton
Sean Hylton
2,401 Points

Chal. 3 of PHP Arrays and Control Structures. Here's my first line: echo "<li>$contacts[0]['name','email']</li>\n";

Can't echo the right output. Please advise.

index.php
<?php
//edit this 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');

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>$contacts[0]['name','email']</li>\n";
echo "<li>$contacts[1]['name','email']</li>\n";
echo "<li>$contacts[2]['name','email']</li>\n";
echo "<li>$contacts[3]['name','email']</li>\n";

?>

4 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

You are getting closer but your concatenation isn't quite right. It's also worth noting that the newline character (\n) will only be interpreted when using double-quoted strings and won't work with single-quotes. Here is an example for the first contact:

<?php
echo "<li>" . $contacts[0]['name'] . " : " . $contacts[0]['email'] . "</li>\n";

Alternatively, you could enclose the array elements in curly braces:

<?php
echo "<li>{$contacts[0]['name']} : {$contacts[0]['email']}</li>\n";

PHP calls this Complex (curly) syntax.

Benjamin Larson
Benjamin Larson
34,055 Points

Each string sequence starts and ends with double-quotes (single quotes could be used for all but the last string that contains the \n character) and is "glued" together by the concatenation operator (.) with the values of the array elements, but the arrays values themselves should not have quotes around them.

Benjamin Larson
Benjamin Larson
34,055 Points

You'll need to grab the values of name and email separately for each array, so treat them like this:

<?php
$contacts[0]['name']
$contacts[0]['email']

You'll also need to match the output exactly how it was before, so you'll have to do some concatenation to format a string that can output the array values within each HTML list item.

Sean Hylton
Sean Hylton
2,401 Points

Thank you for the help, Benjamin. Much appreciated! Unfortunately, I'm still not quite getting it. The third part of the challenge is telling me that I don't have the required output and suggests that I'm modifying it. Below is my best attempt at doing what you instructed, along with some concatenation. Any suggestions?

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";

Sean Hylton
Sean Hylton
2,401 Points

It worked! Much appreciated, Benjamin. Your description was hugely helpful.