Reputation: 57
i have this array:
include("config.php");
$start = "2014-06-20 08:00:00";
$data = mysql_query ("select * from evenement WHERE start = '$start'");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
$arr2[] = $zaznam["resourceId"]; //store query values in second array
}
If i echo $arr2 i get this:
Array ( [0] => STK1 )
now i make condition for array_search:
if (array_search('STK1', $arr2)) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}
but i get this Arr2 not contains STK1 how it is possible? What im doing wrong?
Upvotes: 0
Views: 28
Reputation: 22656
From the array_search documentation:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
array_search
is returning 0 because it found a match at index 0 of the array. This is evaluating to false.
Instead try:
if (array_search('STK1', $arr2) !== false) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}
Upvotes: 1
Reputation: 4190
That is totally correct behaviour for PHP.
The documention for the return value says:
Returns the key for needle if it is found in the array, FALSE otherwise.
In your case you are getting 0 which also evaluates to false in an if.
You have to check if the value is not false using the !==
operator.
if (array_search('STK1', $arr2) !== false) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}
Upvotes: 2