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

I can't catch how to get emails from contacts array

The 3rd task of Challenge makes me really discouraged.

index.php
<?php
//edit this array
/*$contacts = array(
                            'Alena Holligan' => 'alena.holligan@teamtreehouse.com',
                                'Dave McFarland' => 'dave.mcfarland@teamtreehouse.com', 
                'Treasure Porth' => 'treasure.porth@teamtreehouse.com',
                            'Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com'
);
*/
$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 $contacts['email'];
echo $contacts[1]['name'];
echo $contacts[2]['name'];
echo $contacts[3]['name'];
echo "</ul>\n";

1 Answer

Konrad Traczyk
Konrad Traczyk
22,287 Points

Hello Violetta,

Based on code you provided:

  • You have to declare an array before you gonna try to add items to it, like that:
<?php
$contacts = array(); // or
$contacts = []; // these can be used interchangeably but first one is more common
  • After you have some items in your array, you could use foreach instruction to echo all addresses within your arrays like that:
<?php
foreach($contacts as $contact): echo $contact['name'].": ".$contact['email']; endforeach;

Hope it helps,

Konrad