Skip to content
Advertisement

How to merge two array and take only value in first array if there is no matching value in second array

I have two arrays :

$array1 = [460,471];
$array2 = [193,42,471];

I want to take the value only in $array1 if there is no same value in $array2, if there is a same value in $array2 filter it out.

Expected output if no same value available in $array2:

$output = [460, 471]

Expected output is there is same value in $array2:

$output = [460]

Advertisement

Answer

Try array_diff() function

$array1 = [460,471];
$array2 = [193,42,471];

$filtered = array_diff($array1,$array2);


print_r($filtered);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement