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 trialMartin Park
12,792 PointsHelp with Code challenge for Multidimsional Arrays
"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."
" Bummer! Each person in the contact list should have their own internal array. Use another "array()" for each person."
I'm not really sure what I'm doing wrong here. Need a fresh pair of eyes please.
I'm being told to make an array for each person using their name as the key.
I think I have done this but I cannot pass Task 1 of the challenge?
<?php
//edit this array
$contact1 = array(
'Alena Holligan' => 'alena.holligan@teamtreehouse.com',
);
$contact2 = array(
'Dave McFarland' => 'dave.mcfarland@teamtreehouse.com',
);
$contact3 = array(
'Treasure Porth' => 'treasure.porth@teamtreehouse.com',
);
$contact4 = array(
'Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com',
);
//$contacts = array(
// $contact1,
// $contact2,
// $contact3,
// $contact4,
//);
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";
?>
1 Answer
Corey Cramer
9,453 PointsYour followup comment is closer to the mark but in this case you're setting the array key as the person's first name and the value as their e-mail. Below is a nudge in the right direction.
<?php
$contacts = array(
array('name' => 'Alena Holligan'),
array('name' => 'Dave McFarland'),
);
...
In this case if you can deal with the array in a sane way by looking for 'name' without having to know what it is in advance.
Martin Park
12,792 PointsMartin Park
12,792 PointsI also tried this which I think is what the overall challenge is wanting me to do but I think I'm getting ahead of myself now:
$contacts[] = array( 'Alena Holligan' => 'alena.holligan@teamtreehouse.com', );
$contacts[] = array( 'Dave McFarland' => 'dave.mcfarland@teamtreehouse.com', );
$contacts[] = array( 'Treasure Porth' => 'treasure.porth@teamtreehouse.com', );
$contacts[] = array( 'Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com', );