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'm not able to understand the question

<?php //edit this array

$i =['Alena Holligan'=>'alena.holligan@teamtreehouse.com']; $ii =['Dave McFarland'=>'dave.mcfarland@teamtreehouse.com']; $iii=['Treasure Porth'=>'treasure.porth@teamtreehouse.com']; $iv=['Andrew Chalkley'=>'andrew.chalkley@teamtreehouse.com'];

echo "<ul>\n"; //$contacts[0] will return 'Alena Holligan' in our simple array of names. echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n"; echo "<li>Dave McFarland : dave.mcfarland@teamtreehouse.com</li>\n"; echo "<li>Treasure Porth : treasure.porth@teamtreehouse.com</li>\n"; echo "<li>Andrew Chalkley : andrew.chalkley@teamtreehouse.com</li>\n"; echo "</ul>\n"; ?> What is wrong with my code and why it is not passing the 1st task of the quiz

index.php
<?php
//edit this array

  $i =['Alena Holligan'=>'alena.holligan@teamtreehouse.com'];
  $ii =['Dave McFarland'=>'dave.mcfarland@teamtreehouse.com'];
  $iii=['Treasure Porth'=>'treasure.porth@teamtreehouse.com'];
  $iv=['Andrew Chalkley'=>'andrew.chalkley@teamtreehouse.com'];

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n";
echo "<li>Dave McFarland : dave.mcfarland@teamtreehouse.com</li>\n";
echo "<li>Treasure Porth : treasure.porth@teamtreehouse.com</li>\n";
echo "<li>Andrew Chalkley : andrew.chalkley@teamtreehouse.com</li>\n";
echo "</ul>\n";

So, this is the question: In the code below, we have a simple array of contact names. We want to use the $contacts array to fill in the hardcoded list of names and email addresses. To hold the email as well as the name for each contact, we need a multidimensional array, meaning each person in the contact list will have their own array. The first step is to make each person their own single item ASSOCIATIVE array, using 'name' as the key for these internal arrays.

5 Answers

Hi Ahmad,

I think your confusion lies in you missing the keyword "multidimensional" in the challenge question.

A "multidimensional" array is an array of arrays. Your code above has just created four individual arrays.

What you need to do is expand the original array as declared at the start of the challenge

So this:

$contacts = array('Alena Holligan', 'Dave McFarland', 'Treasure Porth', 'Andrew Chalkley');

becomes something like this:

$contacts = array(
['name' => 'Alena Holligan'],
['name' => 'Dave McFarland'],
['name' => 'Treasure Porth'],
['name' => 'Andrew Chalkley']
)

Etc.

Hope that helps, Don :-)

Daniel Marin
Daniel Marin
8,021 Points

Here's the complete answer to the challenge:

<?php
//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')
); 

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

don't forget to use print_r($colors) or var_dump($colors) to see how the array looks

And yes I felt the quiz question is difficult to understand. Don Macarthur shows you the example for the multidimensional array.

Thank you for replying I've tried doing it in a multi-dimensional array but the quiz didn't accept the answer. about your example, won't that assign the different values for the same key; "name".

This is the error message: " Bummer! Each person in the contact list should have their own internal array. Use another "array()" for each person."

Sorry Ahmad, my bad.

I left out the "[]" around each of the internal arrays, plus I had a double quotation mark after Alena's name rather than single one.

I should know better than trying to answer coding questions at 3am after a 10+ hour shift at work...... :-)

Hope it's all sorted for you now. Don

$contacts = array(); $contact[]=array('Alena Holligan' => 'alena.holligan@teamtreehouse.com'); $contact[]=array('Dave McFarland' => 'dave.mcfarland@teamtreehouse.com'); $contact[]=array('Treasure Porth' => 'treasure.porth@teamtreehouse.com'); $contact[]=array('Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com');

This is what I did now and still no good :(.

You're still creating multiple arrays, rather than one array which contains several others.

To create multidimensional arrays in PHP you would write something like this:

$multiDimensionalArray = array(
  ['key' => 'value'],
  ['key' => 'value'],
  ['key' => 'value'],
  ['key' => 'value']
);

If you re-read my original answer this is how I have written the array (I edited it after you pointed out my earlier syntax errors).

As an aside, if you are wondering how my code is formatted to look like a workspace and yours is not, it's because I am using Markdown at the beginning and end of the code.

There is a link to a Markdown Cheatsheet at the bottom of this page and it would be worth looking at and getting used to using it as it makes your code easier to read and more likely for other students to be able to help you.

Hope that helps. Don :-)