-1

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.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
DHRUV GUPTA
  • 2,000
  • 1
  • 15
  • 24
  • Actually, this is normal. Because array_intersect returns a value if there is a intersect. According to your code, this value is 1. The value is `true`as logical. So, your code being like this: `!!true`. Result of this, `true`. in_array method return bool value directly. So, the both codes return same value for your codes. – izniburak Sep 10 '19 at 07:00
  • Why make it so complicated? `$b = in_array('1',[3,2,1]);` - simple & easily readable. – Sougata Bose Sep 10 '19 at 07:06
  • Why this question is down voted? It is quiet interesting – Damian Dziaduch Sep 10 '19 at 07:28

4 Answers4

2

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
  • Again !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.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

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

pinaki
  • 5,393
  • 2
  • 24
  • 32
0

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.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
0

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

thomi
  • 1,603
  • 1
  • 24
  • 31