1

I've a problem with the in_array function.

This is my array: keys can be a mix of string or int like this example.

<?php

$array = array(
    "a" => true,
    "b" => true,
    "c" => true,
    "d" => true,
    0 => "Code",
    1 => "Time",
    2 => "Other"
);

var_dump($array);

if ( in_array("Test", $array) ) { echo "found"; }

This code print ALWAYS found.

Why?

squancy
  • 565
  • 1
  • 7
  • 25
Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33

1 Answers1

9

Because you have true elements in your array and your "Test" string evaluates to true as well.

Try enabling the strict flag like this in_array('Test', $array, true) for a strict comparison.

Tudor
  • 1,798
  • 2
  • 12
  • 21