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

Michael Bennett
PLUS
Michael Bennett
Courses Plus Student 808 Points

Can someone help with replacing these hard coded values with the values in the contacts array?

Can someone help with replacing these hard coded values with the values in the contacts array? I've tried several different things and I can't seem to get the correct output.

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 $contacts[0]['name'];
echo $contacts[0]['email'];
echo $contacts[1]['name'];
echo $contacts[1]['email'];
echo $contacts[2]['name'];
echo $contacts[2]['email'];
echo $contacts[3]['name'];
echo $contacts[3]['email'];



echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";
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";

1 Answer

Benjamin Larson
Benjamin Larson
34,055 Points

Hi Michael-

You've pretty well figured out everything you need. At this point, you just need to take each of the individual names and emails you are echo'ing out and replace the hard-coded values they correspond with in the list items at the bottom.

So for:

<?php
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";

You would replace the hard-coded Alena Holligan with:

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

And then do the same for the email address and repeat for each of the contacts. However, this will fail. If you click on the preview button, you will get a PHP Notice:

PHP Notice: Array to string conversion

this happens because, even though it only outputs 1 item that looks like a string, it is technically still an array since you are working with an array of arrays. So you'll need to breakup the original list-item string into components and concatenate it with the output from the array elements and preserve the same output/spacing as the original.

<?php
echo '<li>' . $name . ' : ' //...you will have to modify and finish this
Michael Bennett
Michael Bennett
Courses Plus Student 808 Points

I tried something new and my output is correct when I preview, but it's still failing. Do you know why? I've pasted my code below.

<?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'
);
$name_alena = 'Alena Holligan';
$email_alena = 'alena.holligan@teamtreehouse.com';
$name_dave = 'Dave McFarland';
$email_dave = 'dave.mcfarland@teamtreehouse.com';
$name_treasure = 'Treasure Porth';
$email_treasure = 'treasure.porth@teamtreehouse.com';
$name_andrew = 'Andrew Chalkley';
$email_andrew = 'andrew.chalkley@teamtreehouse.com';

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>$name_alena : $email_alena</li>\n";
echo "<li>$name_dave : $email_dave</li>\n";
echo "<li>$name_treasure : $email_treasure</li>\n";
echo "<li>$name_andrew : $email_andrew</li>\n";
echo "</ul>\n";
?>