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

Phil Nickel
Phil Nickel
2,077 Points

I don't know how I use these multidemensional array

I do not know how I keep this task done.

index.php
<?php
//edit this array
$contacts = array('Alena Holligan :' => 'alena.holligan@teamtreehouse.com', 'Dave McFarland' => 'dave.mcfarland@teamtreehouse.com', 'Treasure Porth' => 'treasure.porth@teamtreehouse.com', 'Andrew Chalkley' => 'andrew.chalkley@teamtreehouse.com');


//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo $contacts[0];
echo $contacts[1];
echo $contacts[2];
echo $contacts[3];

1 Answer

Jacob Herrington
Jacob Herrington
15,835 Points

The challenge is asking for an associative array. That is an array in which there is a key set by you and a value set by you.

In a simple array, I assign only the values. Here is a simple, non-associative array:

$my_array = array("first", "second", "third");

// if I want to access a member of $my_array
echo $my_array[0]; // will echo the string "first"

In an associative array, I assign keys as well as values using the => operator (aka the T_DOUBLE_ARROW):

$my_associative_array = array("first_key" => "first_value", "second_key" => "second_value");

// if I want to access a member of $my_associative_array
echo $my_associative_array["first_key"]; // will echo the string "first_value"

So in your code you should be making associative arrays using the => operator rather than simple arrays. Then referring to them with the key you assigned.

For example

echo $contacts["Alena Holligan"]; // echos the string "alena.holligan@teamtreehouse.com"

Let me know if this isn't clear enough.