When to use this:-
$a = (true === !!array_intersect(['1'], [3, 2, 1]));
and when to use this
$b = in_array('1', [3,2,1]);
Both returns same.
When to use this:-
$a = (true === !!array_intersect(['1'], [3, 2, 1]));
and when to use this
$b = in_array('1', [3,2,1]);
Both returns same.
array_intersect() returns the common elements form both array. In your code array_intersect(['1'], [3,2,1])
the returns [1]
and when use !!
then it's value is true. Because:
![1]
is false
!false
gets true
Finally (true === !!array_intersect(['1'], [3,2,1]))
return true
, because both sites are true
. Note: ===
is used to check strictly (with type). That means the value of $a
is true
.
Second part of code:
in_array() is used to check weather an element is exist or not in an array. In your code in_array('1',[3,2,1]);
is true
because 1
is exist in array [3, 2, 1]
The difference between array_intersect()
and in_array()
are:
array_intersect($arr1, $arr2)
return an array which are common in both array $arr1
and $arr2
in_array($elm, $arr)
returns a Boolean (true or false) based on the existence of $elm
in the array $arr
Since both parts of your code are getting logically true
, that's why you are getting the same value true
.
in_array is much more readable compared to the array_intersect version of the same. However, array_intersect is much faster compared to in_array. So to answer your question, use array_intersect only if you are dealing with a data set large enough to raise performance issues. Otherwise sticking to in_array will make life easier for the next developer. Cheers
Both are different things.
array_intersect Compare the values of two arrays, and return the matches:
$a1=array(0=>"red",1=>"green",2=>"blue",3=>"yellow");
$a2=array(0=>"red",1=>"green",2=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
Output will be
Array ( [0] => red [1] => green [2] => blue )
in_array is for check if the value exists or not.
$a1=array(0=>"red",1=>"green",2=>"blue",3=>"yellow");
in_array('red',$a); //true
in_array('black',$a); //false
Now, If you want to check the value is exists in an array then you may use in_array it'll return true or false, Or if you want get matches value from two array then you may use array_intersect.
It's not quite the same though. From the docs:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
So, when using array_intersect()
, you will always get true
, but only because the string representation of your second array element "1" matches that of the array you intersect it with. With in_array()
, you can have a third argument, strict
, which is a boolean to indicate whether to also use type checking (see here). So, in your second example, you also have the option of checking whether the type matches, which is more fine-grained.
Personally, I would use the second option in any case just for its readability.
If you want to find elements in an array and get a subarray, then you should go for array_intersect()
for performance reasons see accepted answer to this post