Just learning some PHP - array not working

Hello everyone,

I am new here, and just learning to code.

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”.

Thanks,

CN

if (in_array($car, $cars)) {

in_array() checks if a value exists in an array. a color usually is not a car brand.

Hi,

I don’t understan. Why $car inside the array? I am not using such variable.

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.

then I’ll answer with another question: why should “blue” match “toyota” ?

it makes no sense to check if a colour is inside an array of car brands. plain logic dictates that this will never result in a match.

what you need is

if (in_array('toyota', $cars)) {

Try this:

error_reporting(-1);
ini_set('display_errors',1);

$cars = array
(
  'red'   => 'toyota', 
  'white' => 'nissan',
  'blue'  => 'honda',
); 

$yourColor  = "blue";
$message    = "I don't know the colour of your car"; // DEFAULT
foreach($cars as $carColor => $car) {
  if($yourColor === $carColor) {
    $message = 'Your car is coloured ' .$yourColor .' and is a ' .$car;
  }
}
echo '<h1>' .$message .'</h1>';
echo '<br />';

echo 'color: ' .$yourColor;
echo '<pre>Original $cars: '; print_r($cars); echo '</pre>';

$cars = array_flip($cars);
echo '<pre>Flipping $cars: '; print_r($cars); echo '</pre>';

PHP Manual Array Functions()

Edit:
Just noticed the colour does not match the car because of the way the colours are allocated in the array :frowning:

I will leave you to match the correct colour to the car :smile:

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.

  1. This will help you write clean code
  2. 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 :slight_smile:

$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];
}

I like it, less code and far better than looping through all the array elements.

I prefer setting production:

error_reporting(-1);  //maximum
ini_set('display_errors', false);

// RESET php_error.php
// echo ini_set('error_log', PROJECT_PATH .'php_error.php'); 
// echo ini_get('error_log'); 

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.