Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Arrays and Control Structures PHP Arrays Multidimensional Arrays

What do I do now?

I have done the php arrays part of the course, but am having difficulty wrapping it up as I don't know what to do to make the output a multidimensional array. If anyone can help I will be very grateful. Thanks

PS this is the address of the code challenge in case it doesn't come out: https://teamtreehouse.com/library/php-arrays-and-control-structures/php-arrays/multidimensional-arrays

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

echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo '<li>' . $contacts[0]['name']['email'] . '</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";




?>

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

What do you do now, you ask....

I would recommend doing a little playing around with your array to understand the multidimensional associative array that you created. PHP provides many tools to help you out.

Two immediately come to mind:

  1. var_dump() - see the break out of your variable
  2. json_encode() - turn an array into json data format

Run something like the following:

<?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'),
);

//  use var_dump() to see entire array
var_dump($contacts);
//  or check out the contracts array in json format
echo "\ncontacts array in json format " . json_encode($contacts, JSON_PRETTY_PRINT);
echo "\ncontacts array at forth index is ---> " . json_encode($contacts[3]);
echo "\ncontacts array at  third index, name value is ---> " . $contacts[2]['name'];
echo "\ncontacts array at second index, email value is ---> " . $contacts[1]['email'];

You should see the following output:

array(4) {
  [0] =>
  array(2) {
    'name' =>
    string(14) "Alena Holligan"
    'email' =>
    string(32) "alena.holligan@teamtreehouse.com"
  }
  [1] =>
  array(2) {
    'name' =>
    string(14) "Dave McFarland"
    'email' =>
    string(32) "dave.mcfarland@teamtreehouse.com"
  }
  [2] =>
  array(2) {
    'name' =>
    string(14) "Treasure Porth"
    'email' =>
    string(32) "treasure.porth@teamtreehouse.com"
  }
  [3] =>
  array(2) {
    'name' =>
    string(15) "Andrew Chalkley"
    'email' =>
    string(33) "andrew.chalkley@teamtreehouse.com"
  }
}

contacts array in json format [
    {
        "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"
    }
]
contacts array at forth index is ---> {"name":"Andrew Chalkley","email":"andrew.chalkley@teamtreehouse.com"}
contacts array at  third index, name value is ---> Treasure Porth
contacts array at second index, email value is ---> dave.mcfarland@teamtreehouse.com
Process finished with exit code 0

Does that get you moving in the right direction?

Hello Dave Thank you for your guidance. I eventualy managed by myself but I find that just asking the question gives me help