-2

I have two arrays and the second array is like the following :

$array2 = ['android', 'iphone', 'windowsmobile'];

now the first array can be like either

$array1 = ['web', 'android', 'iphone'];

OR

$array1 = ['android']; // Not web

OR

$array1 = ['web', 'iphone']; // web+android

OR

$array1 = ['android', 'iphone'];

and never be web alone.

and $array2 will be same always.

Now I need to check is there any element in the first array is not there in the second array and to get this value. In the first and third case it is web and for the second case there is nothing.

How this can be done ?

Happy Coder
  • 4,255
  • 13
  • 75
  • 152

1 Answers1

1
<?php


$array2 = ['android', 'iphone', 'windowsmobile'];

$array1 = ['web', 'android', 'iphone'];
print_r(array_diff($array1, $array2));

$array1 = ['android']; // Not web
print_r(array_diff($array1, $array2));

$array1 = ['web', 'iphone']; // web+android
print_r(array_diff($array1, $array2));

$array1 = ['android', 'iphone'];
print_r(array_diff($array1, $array2));

?>

Output

Array(
 [0] => web
)
Array(
)
Array(
 [0] => web
)
Array(
)

See it in action on 3v4l.org

yunzen
  • 32,854
  • 11
  • 73
  • 106