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

Jorge Cid
Jorge Cid
1,300 Points

Greetings, Im stuck in this part of the exercise, how do i manage to add the email value? i tried adding another echo...

I tried adding another echo with the email value of the array and it seems thats not the right answer, i tried a couple of other things but it seems im not getting the right one, so im not really sure what am i missing to get the email value

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

echo "</ul>\n";

2 Answers

You can include the email and other text in the same echo statement by using concatenation. For example:

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

It's so beautiful and good, but less is more. :) What happens is what you want to achieve. I can write the data of the array together. The keys will increase in the same way. Generating data dumps is easier. With these practices, you can work faster.

<?php

$contacts = [
    [
        "name"  => 'Alena Holligan',
        "email" => 'alena.holligan@teamtreehouse.com',
    ],
    [
        "name"  =>'Treasure Porth',
        "email"    =>'treasure.porth@teamtreehouse.com'
    ],
    [
        "name"  =>'Andrew Chalkley',
        "email"    =>'andrew.chalkley@teamtreehouse.com'
    ]
];

?>

<ul>
    <?php foreach ($contacts as $contactValue) : ?>
        <li><?php echo sprintf('%s : %s', $contactValue['name'], $contactValue['email']); ?></li>
    <?php endforeach ?>
</ul>