0

Here are two methods to Get Unique Values From Two Arrays Using PHP.

Method-1

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);

$out1 = array_diff($array1, $array2);
$out2 = array_diff($array2, $array1);
$output = array_merge($out1, $out2);

print_r($output);

Method-2

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);

$new_array = array();
foreach($array1 as $value) {
if(!in_array($value, $array2)) {
array_push($new_array, $value);
}
}
foreach($array2 as $value) {
if(!in_array($value, $array1)) {
array_push($new_array, $value);
}
}

print_r($new_array);
Bikash Changed status to publish January 14, 2022
Add a Comment