1
$blockon2 = array("opi-infinite-shine", "opi-gel", "cnd-vinylux", "opi");

var_dump(in_array(array("opi-infinite-shine", "opi-gel"), $blockon2, true));
// returns false

Someone has some idea of why it's returning false? Thanks!


It searchs for the exactly matching, so to return true is necessary an array inside $blockon2 with the needle ($blockon2[]= array("opi-infinite-shine", "opi-gel"))

4
  • 3
    Why are you casting an array to an array? Commented Aug 7, 2015 at 12:40
  • I read a comment on stack overflow saying to try this. but it does not work with or without the cast. Commented Aug 7, 2015 at 12:41
  • What you think that in_array does? Commented Aug 7, 2015 at 12:46
  • check the values of the first array, to see if any of them is on the second array, returning true or false Commented Aug 7, 2015 at 13:11

1 Answer 1

4

You can use array_diff instead:

$blockon2 = array("opi-infinite-shine", "opi-gel", "cnd-vinylux", "opi");

var_dump(!(bool)array_diff(array("opi-infinite-shine", "opi-gel"), $blockon2));

It will return true if all array entries present in original array

Or if you need to check if any of values exist try array_intersect:

$blockon2 = array("opi-infinite-shine", "opi-gel", "cnd-vinylux", "opi");

var_dump((bool)array_intersect(array("opi-infinite-shine", "opi-gel"), $blockon2));
Sign up to request clarification or add additional context in comments.

8 Comments

You can't use array as needle in in_array - untrue, var_dump(in_array(array(1,2), array(array(1,2),array(3,4)), true)); is true
Quoted from PHP.net Example #3 in_array() with an array as needle. You can use array as needle.
Ok, my bad. Updated my answer
I just need 1 entry to be present, not all of them.
You are right, I should use array_diff, since in array looks for the exactly match, so I can't give different values for the needle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.