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

Aaron Gates
Aaron Gates
1,573 Points

Calling associative array's

Replacing the hard coded name an email address with the call from the array is giving me a lot of trouble. I have looked on the php arrays - manual but that just confused me even more. I have done what it looks like I should have done from W3, but I am stuck and not getting any clues from the challenge that make sense to me. Any help?

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

//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "$contacts[0][0] $contacts[0][1]" . "\n";
echo "$contacts[1][0] $contacts[1][1]" . "\n";
echo "$contacts[2][0] $contacts[2][1]" . "\n";
echo "$contacts[3][0] $contacts[3][1]" . "\n";
echo "\n";

3 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

Aaron Gates you have to make sure your output still match the original output including colon and spacing.

<?php
echo "<li>" . $contacts[0]['name'] . " : " . $contacts[0]['email'] . "</li>\n";
?>
Paolo Scamardella
Paolo Scamardella
24,828 Points

Easier way to print those out is:

echo $contacts[0]["name"] . " " . $contacts[0]["email"] . "\n";
echo $contacts[1]["name"] . " " . $contacts[1]["email"] . "\n";
echo $contacts[2]["name"] . " " . $contacts[2]["email"] . "\n";
echo $contacts[3]["name"] . " " . $contacts[3]["email"] . "\n";

Also, to access an associative array, you need to use the key string instead of a number. The outer array is not an associative array, so it makes sense to access it via a number. The inner array are associative, so you need to access them via the string key such in your case "name" and "email".

Aaron Gates
Aaron Gates
1,573 Points

That worked for the output and your explanation makes sense. For the coding challenge once I inserted this code, the output works for a second, then throws the error that step 1 is no longer passing. That just doesn't make sense.

Paolo Scamardella
Paolo Scamardella
24,828 Points

@Aaron Gates, Sorry, but I don't have access to the exercise, so I tried without looking that up. I didn't know you needed to print out LIs.