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 trialVicki Greer
5,020 PointsStuck on Challenge Task 3 of 3 in PHP Arrays and Control Structures
This is where I'm stuck: Challenge task 3 of 3 in the PHP Arrays and Controls Structures section: "Replace the hard coded values in the output with the name and email values in the contacts array'
I keep getting an array to string conversion error when I try to print out the contact list.
I added some code to print out the list with no HTML in it (although I can't seem to get it to respect my "\n"). If you run the attached code, you'll see that it works that far. So I'm fairly confident that I've found the way to reference a multi-dimensional array.
But in the formatted section with the HTML, instead of printing the name, it prints Array['name'] and Array['email'].
The error I get is: PHP Notice: Array to string conversion in index.php on line 29
I've tried running the code in my workspace, but I get the same error and the HTML code is just echoed to the line instead of being translated to an unordered list.
I've tried surrounding all the PHP with the opening and closing PHP tags.
The error when I submit my code is "Bummer! I do not see the required output. You should not be modifying the output at this point".
Can someone give me some suggestions or tell me where I'm going wrong?
Thanks, Vicki
<?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 "\n\n";
// This code works, except that it doesn't print the newline
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";
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>Treasure Porth : treasure.porth@teamtreehouse.com</li>\n";
echo "<li>Andrew Chalkley : andrew.chalkley@teamtreehouse.com</li>\n";
echo "</ul>\n";
2 Answers
Daniel Amazigo
Courses Plus Student 11,357 PointsHi Vicki,
I actually submitted an answer earlier only to find out that wasn't the answer. Anyway, did some digging and the code I've pasted here is the solution to the challenge. I haven't actually done this challenge before now or been through this part of the PHP course.
<?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";
Strangely, when I did what you were trying to do in the beginning part of your answer to the third task, it didn't work.
I noticed in your code that you were initially on the right path and I also thought that should have been the answer, but still that wasn't quite it.
So after some digging, I came across the above. It seems what the challenge required was that the places where "name" : "email" address once were hardcoded, needed to be replaced with 'references' to them as found in your $contacts array -- which is shown above in the code in parentheses. So, I guess from the first two lines of your answer to the 3rd task, you were missing the parentheses.
Hope you get a better understanding from this. Goodnight : )
Daniel
Mike Wagner
23,559 PointsIt looks like you may have forgotten to wrap your echoes into li
tags. Take a look at the echo statements at the end of your posted code and you'll see how they are wrapped and structured.
Vicki Greer
5,020 PointsVicki Greer
5,020 PointsHi Daniel, Your method makes more intuitive sense than the method I ended up using, which was strongly influenced by the fact that I learned PHP as a part of WordPress before I took this class. But this is the way I got it to work: '''php <?php //edit this array $contacts = array('Alena Holligan', 'Dave McFarland', 'Treasure Porth', 'Andrew Chalkley');
$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', ), ); ?> <ul><?php echo "\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names. ?> <li><?php echo $contacts[0]['name']; ?> : <?php echo $contacts[0]['email']; ?></li> <li><?php echo $contacts[1]['name']; ?> : <?php echo $contacts[1]['email']; ?></li> <li><?php echo $contacts[2]['name']; ?> : <?php echo $contacts[2]['email']; ?></li> <li><?php echo $contacts[3]['name']; ?> : <?php echo $contacts[3]['email']; ?></li> </ul><?php echo "\n"; ?> ''' Pardon me if that didn't come out formatted correctly. I'm new to markdown, too, and there doesn't seem to be a "preview" option.
Thanks, Vicki
John Akers
2,689 PointsJohn Akers
2,689 PointsThanks Daniel! the brackets were the thing I was missing also. '''PHP <?php //edit this array //$contacts = array('Alena Holligan', 'Dave McFarland', 'Treasure Porth', 'Andrew Chalkley'); $contacts[] = [ 'name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com', ];
$contacts[] = [ 'name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com', ]; $contacts[] = [ 'name' => 'Treasure Porth', 'email' => 'treasure.porth@teamtreehouse.com', ]; $contacts[] = [ '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"; '''PHP