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 just have zero idea what to put

whats the answer nothing i do works

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

1 Answer

Joel Bardsley
Joel Bardsley
31,249 Points

Hi Kaelub, I presume you're stuck on Challenge Task 3 of 3 - Replace the hard coded values in the output with the name and email values from the contacts array.

Let's take the first list item:

<?php
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";
?>

Using your array containing the names and email addresses of the four teachers, you need to replace 'Alena Holligan' and 'alena.holligan@teamtreehouse.com' inside the echo statement with the values from your Multidimensional Array. As you know, with a one-dimensional array ie:

<?php
$array = array("value1", "value2", "value3");
?>

To echo "value1" you would do echo $array[0];

Let's change this to a multimensional array:

<?php
$array = array(
  array("key1" => "value1"),
  array("key2" => "value2"),
  array("key3" => "value3")
);
?>

Now, if you did echo $array[0]; it would try to echo the first inner array (array("key1" => "value1")) as a string, which would display the following notice to the screen: PHP Notice: Array to string conversion in filename.php on line xx

So in order to echo "value1" successfully, you need to specify the relevant key inside the first inner array. To do this, you would do echo $array[0]["key1"];

Applying the above to echo 'Alena Holligan' from your array, you need to echo the value of the ["name"] key inside your first inner array and use string concatenation on the relevant list item, ie:

<?php
echo "<li>" . $contacts[0]["name"] . " : alena.holligan@teamtreehouse.com</li>\n";
?>

Hopefully this helps you to get started, just use the same process to include the email address in your echo statement, and then apply the process to the other list items. If you get stuck, try rewatching the previous video.