The array below does not echo “You own a $cars[0], its color is $color”
However if I change, $color in (in_array($color, $cars)) to (in_array(toyota, $cars)), it does echo “You own a toyota, its color is blue”
<?php
error_reporting(E_ALL ^ E_NOTICE);
$cars = array('toyota', 'nissan', 'honda');
$color = "blue";
if (in_array($color, $cars)) {
echo "<H1>You own a $cars[0], its color is $color</H1>";
}
else
{
echo "<H1>I don't know what kind of car you have, so I don't I
don't know the color either.</H1>";
}
?>
How can I make it echo “You own a toyota, its color is blue”.
To get something out of an array, I loop through it and ask each iteration of the loop if it matches what I want. Ask google ‘php loop through array’ and give that a try.
P.S. I think you are looking for a multidimensional array for your car info, this would allow you to make an array full of arrays of cars, and each of those car arrays would have info like color and other options.
I know I am out of topic but, I must say.
Even more that you are a beginner, do not use:
error_reporting(E_ALL ^ E_NOTICE);
On development environment:
error_reporting(E_ALL);
// or, worst case
error_reporting(E_ALL & ~E_STRICT);
Do not ignore notices.
This will help you write clean code
You can have some hidden errors in those notices
On production environment
// always!
error_reporting(0);
And on topic, I think you got the point from the others
Also, check the manual for some examples http://php.net/in_array
LE: @John_Betong you do not need the foreach for your structure
$yourColor = "blue";
$message = "I don't know the colour of your car"; // DEFAULT
if (isset($cars[$yourColor])) {
$message = 'Your car is coloured ' .$yourColor .' and is a ' .$cars[$yourColor];
}