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

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,870 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