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

Hazem mosaad
Hazem mosaad
15,384 Points

why this not work in multi dimes. challenge ?

<?php
//edit this array
$contacts = array('name1'=>array('Alena Holligan,alena.holligan@teamtreehouse.com'), 'name2'=>array('Dave McFarland','dave.mcfarland@teamtreehouse.com'), 
                  'name3'=>array('Treasure Porth','treasure.porth@teamtreehouse.com'),
                  'name4'=>array('Andrew Chalkley','andrew.chalkley@teamtreehouse.com'));
var_dump($contacts);
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";

?>

7 Answers

Matthew Carson
Matthew Carson
5,965 Points

The first challenge only wants you to build the arrays with the 'name' values. You will add 'email' in the second challenge.

<?php
$contacts = array(
                array('name'=>'Alena Holligan'),
                array('name'=>'Dave McFarland'),
                array('name'=>'Treasure Porth'),
                array('name'=>'Andrew Chalkley')
            );
Hazem mosaad
Hazem mosaad
15,384 Points

oh my god i create all in first challenge thank you very much my friend :)

Matthew Carson
Matthew Carson
5,965 Points

Which part are you having trouble passing?

Challenge 2 of 3?

In this challenge they want an array key of 'email' you named it 'Email', the solution is a simple as renaming all of the 'Email' keys to 'email'.

For Challenge 3 of 3 you have to use array access to get the information replacing the hardcoded string. For example:

<?php
echo "<li>{$contacts[0]['name']} : {$contacts[0]['email']}</li>\n"; // would replace
echo "<li>Alena Holligan : alena.holligan@teamtreehouse.com</li>\n"; // this
Marcus Parmentier
Marcus Parmentier
6,571 Points

How did you know to add the brackets before the php values? Was wondering if this is something they taught and I forgot due to lack of use and practice, or if it was something you had to google?

Zulkifli Isah
Zulkifli Isah
2,578 Points

$contacts[] = [ 'name' => 'Alena Holligan', 'email' => 'alena.holligan@teamtreehouse.com', ] $contacts[] = [ 'name' => 'Dave McFarland', 'email' => 'dave.mcfarland@teamtreehouse.com', ] $contacts[] = [ array('name'=>'Treasure Porth', 'email'=>'treasure.porth@teamtreehouse.com', ] $contacts[] = [ 'name' => 'Andrew Chalkley', 'email' => 'andrew.chalkley@teamtreehouse.com', ] echo "<ul>\n";

echo "<li>{$contacts[0]['name']} : {$contact[0]['email']}</li>\n"; echo "<li>{$contacts[1]['name']} : {$contact[1]['email']}</li>\n"; echo "<li>{$contacts[2]['name']} : {$contact[2]['email']}</li>\n"; echo "<li>{$contacts[3]['name']} : {$contact[3]['email']}</li>\n"; echo "</ul>\n";

Matthew Carson
Matthew Carson
5,965 Points

Each 'value' in the arrays need to be given a 'key'.

In your code you are adding the email addresses into the same string as the names.

<?php
var_dump($contacts[$name1]) // Would return "Alena Holligan,alena.holligan@teamtreehouse.com"

There isn't really an easy way to grab the name or email values this way.

What you want to do is break up each piece of information (name and email) like this:

<?php
$contacts = array(
    $name1 => array(
        'name' => 'Alena Holligan',
        'email' => 'alena.holligan@teamtreehouse.com'
    )
);

This way you could access the values like so:

<?php
echo $contacts[$name1]['name'] // Would echo 'Alena Hooligan'
echo $contacts[$name1]['email'] // Would echo 'alena.hooligan@teamtreehouse.com'
Hazem mosaad
Hazem mosaad
15,384 Points

What is wrong with this its work in my wamp server !

<?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 $contacts[0]['name'];
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";
?>
Hazem mosaad
Hazem mosaad
15,384 Points

No the first challenge add each person to array i created it as above work in my computer but not work in challenge

This will do the trick.

$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')
            );