I have two arrays containing repeating values:
$test1 = Array(
"blah1",
"blah1",
"blah1",
"blah1",
"blah2"
);
$test2 = Array(
"blah1",
"blah1",
"blah1",
"blah2"
);
I am trying to get array difference:
$result = array_diff($test1,$test2);
echo "<pre>";
print_r($result);
I need it to return array with single value blah1
, yet it returns empty array instead...
I suspect it has something to do with fact there are duplicate values in both arrays, but not sure how to fix it...
Please help!!
EDIT:
End up writing this function to do the trick:
function subtract_array($array1,$array2){
foreach ($array2 as $item) {
$key = array_search($item, $array1);
unset($array1[$key]);
}
return array_values($array1);
}
Definition and Usage. The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.
Definition and Usage The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.
We can use the array_unique () function to remove duplicate values from an array. The array_unique () function is a specialized function for removing the duplicate values from an array. The correct syntax to use this function is as follows.
PHP array_diff () Function The array_diff () is a built-in function of PHP, and this function is implemented for calculating or comparing the difference between two or more arrays. The comparison between arrays is made based on the values of the element between one or more array, and then their differences are returned as a new array.
The array_diff () is a built-in function of PHP, and this function is implemented for calculating or comparing the difference between two or more arrays. The comparison between arrays is made based on the values of the element between one or more array, and then their differences are returned as a new array.
To anybody wanting a double-sided array_diff - mentioned by rudigier at noxx dot at. Remember, array_diff gives you everything in the first array that isn't in the subsequent arrays. $array1=array('blue','red','green'); $array2=array('blue','yellow','green');
array_diff
compares the first array
to the other array
(s) passed as parameter(s) and return
s an array
, containing all the elements present in the first array
that are not present in any other array
s. Since $test1
and $test2
both contain "blah1"
and "blah2"
, and no other values, actually, the expected behavior of array_diff
is the one that you have experienced, that is, to return
an empty array
, since, there is no element in $test1
which is not present in $test2
.
Further read. Also, read some theory to understand what you are working with.
Spotted a problem with Acidon's own solution. The problem comes from the fact that unset($array[false])
will actually unset $array[0]
, so there needs to be an explicit check for false
(as David Rodrigues pointed out as well.)
function subtract_array($array1,$array2){
foreach ($array2 as $item) {
$key = array_search($item, $array1);
if ( $key !== false ) {
unset($array1[$key]);
}
}
return array_values($array1);
}
Some examples
subtract_array([1,1,1,2,3],[1,2]); // [1,1,3]
subtract_array([1,2,3],[4,5,6]); // [1,2,3]
subtract_array([1,2,1],[1,1,2]); // []
subtract_array([1,2,3],[]); // [1,2,3]
subtract_array([],[1,1]); // []
subtract_array(['hi','bye'], ['bye', 'bye']); // ['hi']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With