Accessing Associative Arrays


#1

please help me
what is problem this code
<?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 />';
        
    echo $myAssocArray['colour'];
    echo '<br />';
</p>

please i can not understand this lesson


#2

any body there? please help me


#3

no want to help i also solve my self thanx


#4

What part you didn't understand?

In first array, keys are numeric (0, 1, ...)(and they also represent indexes in array) and echo will output element that has key with value of 1 in that array.
In second array, keys are not numeric values but named strings (year, colour, ...). In this array we print the values with specifying the key name instead of its position in array.


#5

Try this code.

<?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'] . ' ' . $myAssocArray['make'] . '. It is ' . $myAssocArray['colour'] . ' and has ' . $myAssocArray['doors'] . ' doors.';
  ?>