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

Task3 broken?

Anyone having a problem with this? I cannot get task 3 to complete. getting an error saying I should not be modifying the output code even though that is what the task is asking me to do.

index.php
<?php
//edit this array
//$contacts = array('Alena Holligan', 'Dave McFarland', 'Treasure Porth', 'Andrew Chalkley');

$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'
];

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

2 Answers

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi Chris Edwards,

You are doing really great:) No task 3 is not broken. I don't have very good explanation but PHP doesn't like variables/expresions in strings who have string inside them for example multidimensional array $contacts[0]['name'] - name is in single quates as string. So there is two options you can use Complex (curly) syntax which is something like this.

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

Or I would use a most common way which is concatenation

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

You can always check out in preview tab results and see if there is any errors. In the top right corner there is "Preview" button.

While Algirdas Lalys is correct, I'm going to also answer with an alternative. I don't like the messy concatenation and extra quotation marks. For me it's just one more place that I can mess up. Instead I would use the Complex (curly) Syntax to insert the variables like so:

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

I would definitely check out that link and read up on how you can do various things with strings and variables. You'll thank yourself later.

jopapadaki
jopapadaki
7,311 Points

Too many trials & errors but didn't think this, thank you!