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

Please help me figure out what's wrong

Hello,

I am getting the following error on my code in the code challenge:

Bummer: You should have a multi-dimentional array (array of arrays), whose outermost array is 0 indexed. Example: $contacts = array(array('name'=>'Alena Holligan'));

The question is:

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 BOTH the email as well as the name for each contact, we need a multidimensional array. Each person in the contact list will have their own inner array. The inner array will be an associative array of name and email.

The first step is to add a second inner array to the $contacts array. Instead of each value being a string containing a name, each value should be an ASSOCIATIVE array. Use the string 'name' as the key and their actual name as the value for these internal arrays.

As you can see in my code I have tried my best to answer the question but can't figure out what I'm doing wrong. I have tried to remove the names from the array key but it comes up with an error too.

Any help would be much appreciated.

index.php
<?php
//edit this array
$contacts['Alena Holligan'] = [
  'name' => 'Alena Holligan',
  'email' => 'alena.holligan@teamtreehouse.com',
];

$contacts['Dave McFarland'] = [
  'name' => 'Dave McFarland',
  'email' => 'dave.mcfarland@teamtreehouse.com',
];

$contacts['Treasure Porth'] = [
  'name' => 'Treasure Porth',
  'email' => 'treasure.porth@teamtreehouse.com',
];

$contacts['Andrew Chalkley'] = [
  '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>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";

3 Answers

adamdonatello
adamdonatello
27,485 Points

Hi newtocoding,

As mentioned in your error message, The task requires you to create a multi-dimentional array in the following format:

$contacts = array(array('name'=>'Alena Holligan'));

There are a couple of ways that you could write the syntax to complete this task. One of them being like so:

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

I hope this helps!

ITAY GAZIT
ITAY GAZIT
12,159 Points

<?php //edit this array $contacts = array( array('name' => "Alena Holligan"), array('name' => "Dave McFarland"), array('name' => "Treasure Porth"), array('name' => "Andrew Chalkley") );

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";

Hi newtocoding,

I had the same issue with this exercise. In my case the short array syntax was not working ( "[]" ) introduced in PHP 5.4 Even though both versions are valid the second one is the correct answer.

<?php
// This returns an error
$contacts = [
  ['name' => 'Alena Holligan'], 
  ['name' => 'Dave McFarland'], 
  ['name' => 'Treasure Porth'],  
  ['name' => 'Andrew Chalkley']
];

// This result is validated as correct
$contacts = array(
  array('name' => 'Alena Holligan'), 
  array('name' => 'Dave McFarland'), 
  array('name' => 'Treasure Porth'),  
  array('name' => 'Andrew Chalkley')
);