Can somebody explain why in_array()
is returning false for the following example:
$arrA=array('apple');
$arrB=array(
'first'=>'banana',
'second'=>'apple'
);
var_dump(in_array($arrA,$arrB)); // false
On a related note: If $arrA
had more than one element, how would I check if any/at least one value of $arrA
is in $arrB
?
Thank you!
in_array($item, $array)
tests whether $item
is one of the values in $array
. The values in $arrB
are strings, but $arrA
is a whole array. An array is not the same thing as a string, even if the array just contains a single element that's a string. You could use:
in_array($arrA[0], $arrB) // true
If you want to test whether the two arrays have any elements in common, use:
count(array_intersect($arrA, $arrB)) > 0
The purpose of the in_array
function is to determine if a single value (the 'needle') exists within an array (the haystack
).
What you seem to want to do is find where one or more values from one array exist within another. This is known as intersection, and can be done in PHP using array_intersect
. http://www.php.net/manual/en/function.array-intersect.php
You search for array, not string:
$arrA=array('apple');
$arrB=array(
'first'=>'banana',
'second'=>'apple'
);
var_dump(in_array($arrA,$arrB)); // false
var_dump(in_array('apple',$arrB)); // true
in_array
first param needle
type is mixed
.
You can do it like this:
var_dump(in_array('apple', array_values($arrB))); // true
// or
var_dump(in_array('apple', $arrB)); // true
If you want to check multiple items, you can make a helper function like this:
$arrB=array(
'first'=>'banana',
'second'=>'apple'
);
function checkInArray($toCheck, $sourceArray) {
$totalIn = 0;
foreach ($toCheck as $k => $v) {
if (in_array($v, $sourceArray))
$totalIn++;
}
return ($totalIn == sizeof($toCheck));
}
var_dump(checkInArray(array('apple','banana'), $arrB)); // true
Using array_intersect()
can do you work
$arrA=array('apple');
$arrB=array(
'first'=>'banana',
'second'=>'apple'
);
if(array_intersect($arrA, $arrB))
echo"yes exist";
read more about array_intersect
Some of these other answers seem to have skimped on the documentation, specifically this note:
Changelog
Version Description
4.2.0 needle may now be an array.
The issue is that when you're searching for an array within an array, you're not looking for each item in needle, you're looking for needle as a whole.
In your case, $arrA
is array('apple')
, so in_array
looks for array('apple')
within $arrB
.
The reason the return value is false
is because array('apple')
isn't in $arrB
as a whole even though the contents are.
This example should help clear things up a bit:
$arrA = array('apple');
$arrB = array(
'first' => 'banana',
'second'=> 'apple'
);
$arrC = array(
'first' => 'banana',
'second'=> array('apple')
);
var_dump(in_array($arrA, $arrB)); // false
var_dump(in_array($arrA, $arrC)); // true
If you're looking to see which items in $arrA
exist in $arrB
, then you should use array_intersect.
in_array checks for exact array match match
$arrA=array('apple');
$arrB=array(
'first'=>'banana',
'second'=>'apple'
);
$arrC=array(
'first'=>array('banana'),
'second'=>array('apple')
);
echo var_export(in_array($arrA,$arrB),true) . "<br/>
"; // false
echo var_export(in_array($arrA[0],$arrB),true) . "<br/>
"; // true
echo var_export(in_array($arrA,$arrC),true) . "<br/>
"; // true