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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,288 Points

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";
Pihe Edmond
Pihe Edmond
2,315 Points

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>