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 trialUnsubscribed User
8,428 PointsHow to get correct value in a multi-dimensional array with associative arrays?
I set the code according to the proposed formatting however I am getting an error. Can someone help me see what I am missing here?
<?php
//edit this array
$contacts = array(
['name'=>'Alena Holligan', 'email'=>'alena.holligan@teamtreehouse.com'],
['name'=>'Dave McFarland', 'email'=>'dave.mcfarland@teamtreehouse.com'],
['name'=>'Treasure Porth', 'email'=>'treasure.porth@teamtreehouse.com'],
['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> $contact[0]['name'] : $contact[0]['email']</li>\n";
echo "<li> $contact[1]['name'] : $contact[1]['email']</li>\n";
echo "<li> $contact[2]['name'] : $contact[2]['email']</li>\n";
echo "<li> $contact[3]['name'] : $contact[3]['email']</li>\n";
echo "</ul>\n";
Unsubscribed User
8,428 PointsYes, I checked preview. However I am set up mostly in a mamp environment writing code in sublime and viewing through a web browser to see results. The code is not working as I was expecting. I am unsure how to correct the syntax to call for the correct value. I thought that all I needed to do was reference the key from the associative array after referencing the key from the top layer of the first array. What am I missing?
Unsubscribed User
8,428 PointsSimple error was that I had var named wrong but fixing that only created a new error. I am still not getting values to show. New error is about "Array to string conversion in index.php". I still don't really get how to output these values.
1 Answer
KRIS NIKOLAISEN
54,971 PointsTry concatenation. Like this:
echo "<li>" . $contacts[0]['name'] . " : " . $contacts[0]['email'] . "</li>\n";
Unsubscribed User
8,428 PointsThanks, that looks like it will do the job. I just wasn't connecting the syntax.
KRIS NIKOLAISEN
54,971 PointsKRIS NIKOLAISEN
54,971 PointsDid you check the preview?