24

I have an array in PHP

$permission = array( "admin", "moderator", "guest"  );

and i have another array

$userRoles = array( "admin", "moderator" );

I checked with in_array but it doesn't work with multiple values.

How can i check atleast one value in $userRoles exists on $permission without looping ?

Thanks in advance.

2 Answers 2

41

Use array_intersect

count(array_intersect($permission, $userRoles));
Sign up to request clarification or add additional context in comments.

1 Comment

There's not need to call count() unless you really need the count. You can just use if (array_intersect($permission, $userRoles)) ....
14

Use array_intersect

array_intersect — Computes the intersection of arrays

array array_intersect ( array $array1 , array $array2 [, array $ ... ] )

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Read

1 Comment

@Red thanks buddy...i just do not want giving solution but also want to make you learn.

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.