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

Akasha Fryman
Akasha Fryman
2,068 Points

Please advise how this code is incorrect?

The instructions state: Replace the hard coded values in the output with the name and email values from the contacts array.

index.php
<?php
//edit this array
$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' => 'alena.holligan@teamtreehouse.com',
  ];

echo "<ul>\n";
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";

5 Answers

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

Yes the problem is that your including arrays into the string :

In php you cannot do :

<?php echo "some text $array[$i][$j]";?>

It doesn't work because php interprets $array and [$i][$j] as separate strings, you have to concatenate :

try this

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

It should work.

Hi Alexandre,

It's possible to have the array values evaluated inside the string but they have to be enclosed within curly braces.

Example:

echo "<li>{$contacts[0]['name']} : {$contacts[0]['email']}</li>\n";
Alexandre Babeanu
Alexandre Babeanu
10,947 Points

Thanks for the insight Jason I did not know that.

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

Hello,

You have forgotten to remove the names between the li tags.

You did it for the first one. you just have to remove Dave Mc Farland, Treasur Porth and Andrew Chalkey.

Akasha Fryman
Akasha Fryman
2,068 Points

Sorry, I have updated the code above, and it doesn't work. I pasted the wrong piece when writing this original post.

Here's also a screenshot - for some reason, the 'string' brackets aren't turning white even though they are formatted correctly. I think it's a problem with the code system recognizing the code I've put in.

https://www.dropbox.com/s/5o2x8bb74l8dqyq/Screenshot%202017-01-21%2022.05.55.png?dl=0

It outputs this error when testing:

Bummer! I do not see the required output. You should not be modifying the output at this point.

<li>Array['name'] : Array['email']</li>
PHP Notice: Array to string conversion in /home/treeh ouse/workspace/test.php on line 24

Notice: Array to string conversion in /home/treehouse/ workspace/test.php on line 24
PHP Notice: Array to string conversion in /home/treeh ouse/workspace/test.php on line 24

Notice: Array to string conversion in /home/treehouse/ workspace/test.php on line 24
<li>Array['name'] : Array['email']</li>
PHP Notice: Array to string conversion in /home/treeh ouse/workspace/test.php on line 25

Notice: Array to string conversion in /home/treehouse/ workspace/test.php on line 25
PHP Notice: Array to string conversion in /home/treeh ouse/workspace/test.php on line 25

Notice: Array to string conversion in /home/treehouse/ workspace/test.php on line 25
<li>Array['name'] : Array['email']</li>
</ul>

Akasha Fryman
Akasha Fryman
2,068 Points

Thank you for your help, however, it still gives me the error Bummer! I do not see the required output. You should not be modifying the output at this point.

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

try to change Andrew chalkey's email

Akasha Fryman
Akasha Fryman
2,068 Points

It worked - thank you very much. Are there any other ways to achieve the same output within the list format? I never would have guessed to use as many "." in the concatenation.

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

Please mark the answer as correct so your question be marked as solved and so I can get credit :) I don't think there is any other way but normally this would be done with a foreach loop... You will soon be learning about these! You'll see it is much faster with loops.

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

you just have to click on the green check mark under my reply...