array_diff() is a very practical array function in PHP. Its function is to return values in an array that exist in the first array but not in other arrays. Although its syntax seems simple, it often appears when it is actually used, "Why is the result wrong?" This article will summarize common error usage and debugging techniques to help you use array_diff() more efficiently.
$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "cherry"];
$result = array_diff($array1, $array2);
print_r($result);
Output:
Array
(
[0] => apple
)
As you can see, array_diff() compares values, regardless of the key name, and returns the part that is in $array1 but not in $array2 .
$array1 = [1, 2, 3];
$array2 = ["2", "3"];
$result = array_diff($array1, $array2);
print_r($result);
Expected output:
Array
(
[0] => 1
)
Actual output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
This is because array_diff() uses non-strict comparisons (equivalent to == instead of === ). In some cases, strings and numbers do not convert each other successfully, resulting in a comparison failure.
Solution: You can use array_udiff() with a custom comparison function to achieve "strict comparison":
function strict_compare($a, $b) {
return ($a === $b) ? 0 : 1;
}
$result = array_udiff($array1, $array2, 'strict_compare');
print_r($result);
$array1 = ["apple", "banana", "apple"];
$array2 = ["banana"];
$result = array_diff($array1, $array2);
print_r($result);
Output:
Array
(
[0] => apple
[2] => apple
)
Note: array_diff() will not deduplicate, it will retain all unexcluded values and their original key names in the original array.
$array1 = ["a" => "apple", "b" => "banana"];
$array2 = ["banana"];
$result = array_diff($array1, $array2);
print_r($result);
Output:
Array
(
[a] => apple
)
Even if the array is an associative array, array_diff() only compares values and does not compare keys. The key name is just preserved in the result.
Print intermediate variables
var_dump($array1, $array2);
Make sure the two arrays you pass in are free of unexpected string/number mixing, spaces or formatting issues.
Check the data structure using json_encode()
echo json_encode($array1);
Helps quickly see the type of the value or whether there are implicit characters (such as spaces, line breaks, etc.).
Try debugging the function If you use array_udiff() , don't forget to add logs or debug statements to view the comparison of values in the comparison function.
$urls = [
"https://m66.net/page1",
"https://m66.net/page2",
"https://m66.net/page3"
];
$visited = [
"https://m66.net/page2",
"https://m66.net/page3"
];
$unvisited = array_diff($urls, $visited);
print_r($unvisited);
If one of the URLs has a trailing space or a small difference like / , it will also lead to inability to compare correctly!
It is recommended to clean the data and then compare it:
$urls = array_map('trim', $urls);
$visited = array_map('trim', $visited);
$unvisited = array_diff($urls, $visited);
array_diff() is a powerful tool, but because it uses non-strict comparison and only values to compare, it is easy to make errors in the case of inconsistent data types or formats. Only by understanding its underlying behavior and combining appropriate debugging skills can we truly use it well.
I hope this article can help you troubleshoot and solve the problem of "why the result is wrong" you encounter when using array_diff() . If you encounter more complex scenarios or comparison needs, you can also consider combining array_udiff() or custom logic to achieve more granular control.