Should I double check my answer?


#1

After much headache (since the answer forums have now changed ~_~) I finally have a passing code "Advanced Arrays" Lesson 7.7:
<?php
// On the line below, create your own associative array:
$myArray = array(29 => "Hearts",
13 => "Rubies");

  **// On the line below, output one of the values to the page:**
 **echo $myArray['Hearts'];**
 
      
  **// On the line below, loop through the array and output**
  **// *all* of the values to the page:**
 **foreach ($myArray as $number=>$items) {**
     **echo "I have " . $number . " of " . $items . " on my list.";**
 **}**

In result screen, it shows the output like this:
**Undefined index: Hearts (line 13)I have 29 of Hearts on my list.I have 13 of Rubies on my list.**

Should I be worried about the italic part, or is my code fine as it is?


#2

We use the associative arrays to store key -> value pairs.

There are two pairs in your array:

29 => "Hearts",
13 => "Rubies"

On the left side, we have keys, on the right side values.

It should be clear for your that we are indexing arrays by keys, not by values. That is why keys have to be unique.

So, if you want to retrieve value from the first pair you should change your code to:

 echo $myArray[29];