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

Beto Najera
Beto Najera
7,072 Points

I don't understand how to solve this and i've tried many ways.

I have some problems with this challenge.

index.php
<?php
//edit this array
$contact1 = array('Alena Holligan' => 'alena.holligan@teamtreehouse.com');
$contact2 = array('Dave McFarland' => 'dave.mcfarland@teamtreehouse.com');
$contact3 = array('Treasure Porth' => 'treasure.porth@teamtreehouse.com');
$contact4 = array('Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com');

$contacts = array($contact1, $contact2, $contact3, $contact4);

var_dump($contacts);

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

Instead of adding 4 separate $contact arrays, you need to add separate arrays into the same contact array using 'name' as the key.

<?php
//edit this array
$contacts = array(
['name' => 'Alena Holligan'], 
['name' =>'Dave McFarland'], 
['name' =>'Treasure Porth'], 
['name' =>'Andrew Chalkley']
);

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

then add the email as the key

<?php
//edit this array
$contacts = array(
  ['name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com' ], 
  ['name' =>'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com'], 
  ['name' =>'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com'], 
  ['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>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";

finally, concatenate the variables with the strings in the echo

<?php
//edit this array
$contacts = array(
  ['name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com' ], 
  ['name' =>'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com'], 
  ['name' =>'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com'], 
  ['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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there Beto,

The echos that you;ll use to display the data have been provided for you, as well as the values. What you have to do is find a way to display the values dynamically rather than just as hard coded strings.

This is simple enough to do. HINT.

  • You need to display the values by their key `e.g. $array["value"};
  • You'll also need to concatenate the strings as if you try to includes the values as part of a single string it won't work.
  • Have a look at the strings in the echo and where these match up with the values in the $contacts array.

Good luck!

Beto Najera
Beto Najera
7,072 Points

Thanks you both for your help. I can't solve that yet but I'll keep in mind what you say