compare values of two arrays php [duplicate]

I have two arrays:

First one:

array (size=6)
  0 => string '3' (length=1)
  1 => string '4' (length=1)
  2 => string '5' (length=1)
  3 => string '7' (length=1)
  4 => string '8' (length=1)
  5 => string '9' (length=1)

Second one:

array (size=3)
  0 => string '3' (length=1)
  1 => string '4' (length=1)
  2 => string '9' (length=1)

I need to compare these two arrays, and store the matching values in another array matching. Those who don't match should be stored in not_matching.

How should I get this done? Are there functions available for this purpose?

Thanks for the help!


Solution 1:

For matching http://www.w3schools.com/php/showphp.asp?filename=demo_func_array_intersect

$result=array_intersect($array1,$array2);
print_r($result,1);

For not matching http://www.w3schools.com/php/showphp.asp?filename=demo_func_array_diff

$result=array_diff($a1,$a2);
print_r($result);

For custom code

$match_array = array();
$un_match_array = array();
foreach( $array1 as $arr )
{
  if( in_array($arr, $array2) )
  {
      $match_array[] = $arr;
  }
  else
  {
     $un_match_array[] = $arr;
  }
}
print_r($match_array,1);
print_r($un_match_array,1);

Solution 2:

To get the matching results : array_intersect()

https://php.net/array_intersect

To get the not matching results : array_diff()

https://php.net/array_diff