0

I've two arrays, one is my define array and another I process from the database. Is there any proper way to compare between those arrays?

From database:

$user_type = [0 => "public" 1 => "10x413" 2 => "12x432"]

Defined array:

$specificUser = ['10x410','10x411','10x412','10x413','10x414'] 

If any element matches, then return true, just like php in_array() function.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Rafid Shahriar
  • 98
  • 1
  • 1
  • 11
  • Show an example of the two arrays that have a common element. – AbraCadaver Dec 18 '16 at 04:58
  • I've processed this array from database, $user_type = [ 0 => "public" 1 => "10x413" 2 => "12x432"] , and $specificUser = ['10x410','10x411','10x412','10x413','10x414'] this is my own define array – Rafid Shahriar Dec 18 '16 at 05:03

2 Answers2

1

Just loop and check as you would using in_array() to get the value that matches:

foreach($specificUser as $value) {
    if(in_array($value, $user_type)) {
        echo $value;
        //break; to stop checking, a match was found, or not to continue and see more
    }
}

Or to just test for any match:

if(array_intersect($specificUser, $user_type)) {
    // it's true :-)
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

I'm sure this has been asked and answered before, but here's a solution:

array() !== array_intersect($user_types, $specific_users);

Note: The array elements are compared using strict comparison, so two elements are considered matching only if they are the same type and value.

StevenW
  • 27
  • 2
  • 7