$array_name=array(1,2,3,4,5,6,7,8,9,0);
var_dump(in_array("a",$array_name));
why I get true?
but i will get false from
var_dump(in_array("a",$array_name,true));
答案 0 :(得分:2)
With your exact posted code, you cannot possibly be getting true:
php > var_dump(in_array("a", array(1,2,3,4,5,6,7,8)));
bool(false)
But if you had a false-y value in the array:
php > var_dump(in_array("a", array(0,1,2,3,4,5,6,7,8)));
^----- false-y value
bool(true)
php > var_dump(in_array("a", array(0,1,2,3,4,5,6,7,8), true));
bool(false)
Then you would get your expected results. Passing the 3rd true
argument to in_array
forces a strict equality test internally, e.g. ===
instead of ==
:
php > var_dump("a" == 0, "a" === 0);
bool(true)
bool(false)
答案 1 :(得分:0)
Firstly you have different arguments in in_array()
function.
in first call you have strict = false
by default in second it's true
This does not means you should get true because false
is proper returned value
Edit:
I thought about code you give in comment and I assume "aaa" is casted to integer 0 then 0 is found in array so it returns true;
Try to remove 0 from your array and it will return false.
in_array()
has pretty unexpected behavior that is why it is good to use strict option set to true.
<?php
$array = array(1,2,3,4,5,6,7,8,9,0);
$array2 = array(1,2,3,4,5,6,7,8,9);
var_dump(in_array("aaa", $array, false));
var_dump(in_array("aaa", $array2, false));
?>
Output:
bool(true) bool(false)