Bug in Accessing Associative Arrays


#1



Whatever I put in, I get a fault and the error messages don't make sense. One thinks that I did not use [] and the other one thinks I've only said that it's blue. Is this a bug?


echo 'I have a ' . $MyAssocArray['year'] . ' ' . $MyArray[3] . '. It has ' . $MyAssocArray['doors'] . ' doors and is $MyArray[1] . '.';


Oops, try again.
Try talking about the year your car was made, its make, or the number of doors it has. We already know it's blue!


Replace this line with your code.


#2

$MyArray[1]//wrong

change it
because they already know it's blue! try to print year not the color :slight_smile:


#3

either way, the blue doesn't make or break the code. It's getting an error either way.


#4

can you please post a screen shot or entire code?


#5

Sure thing!

<p>
  <?php
    // This is an array using integers as the indices...
    $myArray = array(2012, 'blue', 5, 'BMW');

    // ...and this is an associative array:
    $myAssocArray = array('year' => 2012,
                    'colour' => 'blue',
                    'doors' => 5,
                    'make' => 'BMW');
        
    // This code will output "blue".
    echo $myArray[1];
    echo '<br />';
        
    // Add your code here!
      echo 'I have a ' . $MyAssocArray['year'] . ' ' . $MyArray[3] . '. It has ' . $MyAssocArray['doors'] . ' doors.'; 
  ?>
</p>

#6

'M' should be lowercase because

your variable name is

$myAssocArray

but you wrote to echo

$MyAssocArray