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 trialAbson Chifodya
11,817 PointsAdd a second element with the key 'email' to the internal array of each person. You'll find the correct email for each p
Add a second element with the key 'email' to the internal array of each person. You'll find the correct email for each person in the hard coded output. Where am i getting it wrong--may someone help please.
<?php
//edit this array
$contacts = array(
array('name'=>'Alena Holligan'
array_push($contacts,'email'=>'alena.holligan@teamtreehouse.com')),
array('name'=>'Dave McFarland'
array_push($contacts,'email'=>'dave.mcfarland@teamtreehouse.com')),
array('name'=>'Treasure Porth'
array_push($contacts,'email'=>'treasure.porth@teamtreehouse.com')),
array('name'=>'Andrew Chalkley'
array_push($contacts,'email'=>'andrew.chalkley@teamtreehouse.com.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";
1 Answer
Heidi Brebels
23,239 PointsHello Abson,
You can define arrays in two ways. Depending on preference and PHP version:
1) $contacts = array();
2) $contacts = [];
The second one is supported as of PHP 5.4
You can't put a function (array_push) inside an array definition. You are using an array that contains associative arrays. The outer array will automatically use keys like 0, 1, 2, ... In the associative arrays you define the key (and value) yourself. If you have to add the key 'email' to the definition, just add it like the key 'name'. Items in an array are separated with a comma.
$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.com'
)
);
Ben Payne
1,464 PointsBen Payne
1,464 PointsHey Abson - Your array should look like this:
Hope that helps.
Cheers,
Ben