4/7 advanced arrays


#1

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

Not sure what's wrong


#2

You are trying to access elements from an associative array called $assocCar but you have not defined it anywhere. you have only defined $myAssocArray


#4

// Add your code here!
echo 'I have a ' . $myAssocArray['year'] . ' ' . $myAssocArray['make'] . '. It is ' . $myAssocArray['colour'] . ' and has ' . $myAssocArray['doors'] . ' doors.';


#5

its work for me................


#6

I am struggling with this, please correct me. Thanks

echo 'J'ai une voiture'.'$myAssocArray['year'].''.$myAssocArray['make'].'et possède'$myAssocArray.['doors'].'portes.';


#7

Maybe you can try this:

echo "J'ai une voiture" . " " . $myAssocArray['year'] . " " . $myAssocArray['make'] . " " . "et possede" . " " . $myAssocArray['doors'] . " " . "portes.";

the " " are used as spaces to make it more readable. You can also intregrate this within the string itself. I hope this helps.


#8

Thanks for your reply.

the last code applied

echo 'j ai une voiture'. $myAssocArray['year']. $myAssocArray['make'].'et possède'.$myAssocArray['doors'].'portes.';

the result on the screen was:
j ai une voiture2012BMWet possède5portes.

it worked!!!!


#9

Glad I could help. To correct the spaces, just add them in your strings :wink: