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

Bibiana Fischer
Bibiana Fischer
906 Points

challenging task 3 of 3 I didn't understand how the correct way to code in the output to show the name and email

They said that I should use like $array[#][string] I try to put the numbers of the arrays like 0 1 but didn't work, and I try to put the string name and email together but also show a error, someone can help understand that? Thank you!

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'=> 'andrew.chalkley@teamtreehouse.com'
  ];

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo $contacts[0];
echo $contacts[1];
echo $contacts[2];
echo $contacts[3];

1 Answer

Jason Tame
Jason Tame
5,033 Points

Once those items have been added to the $contacts array, it looks like this:

    [0] => Array
        (
            [name] => Alena Holligan
            [email] => alena.holligan@teamtreehouse.com
        )

    [1] => Array
        (
            [name] => Dave McFarland
            [email] => dave.mcfarland@teamtreehouse.com
        )

    [2] => Array
        (
            [name] => Treasure Porth
            [email] => treasure.porth@teamtreehouse.com
        )

    [3] => Array
        (
            [name] => Andrew Chalkley
            [email] => andrew.chalkley@teamtreehouse.com
        )

It's 4 arrays nested within one larger array. To access a particular one, you need to use its reference number, so $contacts[0] for the first nested array, $contacts[1] for the second one, etc.

Once you have selected the right array, you need to then choose what you want to echo from that array. You have two choices here, 'name' or 'email'. So, to select the name from the second array, you would do something like:

  <?php
    echo $contacts[1]['name'];
  ?>

Which will return 'Dave McFarland'.

Hope that clears it up!

Bibiana Fischer
Bibiana Fischer
906 Points

oh ok I got it, I was trying to put 'name' and 'email' in the same echo, so how can I do to show all of the names and emails of the array contacts?

I did this to test and it works but I'm not sure if is the right way:

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

Thank you so much for your help! :)