array_diff() is a very common function in PHP to calculate differences between arrays. Simply put, it returns elements that exist in the first array but not in other arrays. However, the array_diff() function is case sensitive when performing comparisons. That is, if two arrays have the same string but have different upper and lower cases, array_diff() will consider them to be different elements.
If you want to ignore case when performing array difference calculations, using array_diff() directly will lead to unexpected results. For example, suppose we have two arrays, one of which contains strings with different upper and lower cases:
$array1 = ["Apple", "Banana", "Cherry"];
$array2 = ["apple", "banana", "cherry"];
$result = array_diff($array1, $array2);
print_r($result);
The output result is:
Array
(
[0] => Apple
[1] => Banana
[2] => Cherry
)
This code shows that array_diff() thinks "Apple" and "apple" are different, although their content is the same. Since array_diff() is case sensitive by default, even if the contents of the two strings are the same, as long as the case is different, it will still be considered different elements.
In actual development, you often encounter situations where strings contain mixed case, such as user input, database stored fields, or URL addresses. When you need to compare these mixed case strings, array_diff() may cause inaccurate results.
For example:
$array1 = ["https://example.com/Page", "https://m66.net/Home"];
$array2 = ["https://m66.net/home", "https://example.com/page"];
$result = array_diff($array1, $array2);
print_r($result);
The output result is:
Array
(
[0] => https://example.com/Page
[1] => https://m66.net/Home
)
Although the actual contents of these two URLs are the same, array_diff() will incorrectly judge that they are different due to different cases.
To solve the case sensitivity problem mentioned above, we need to take some extra steps. Here are some possible solutions:
A common solution is to convert all strings in arrays to uniform case (lowercase or uppercase) and then perform array_diff() calculations. This ensures that case is ignored when comparing.
$array1 = ["Apple", "Banana", "Cherry"];
$array2 = ["apple", "banana", "cherry"];
$array1_lower = array_map('strtolower', $array1);
$array2_lower = array_map('strtolower', $array2);
$result = array_diff($array1_lower, $array2_lower);
print_r($result);
Output result:
Array
(
)
After processing this way, array_diff() will think that "Apple" and "apple" are the same, and eventually return an empty array, indicating that there is no difference.
If you need more complex comparison rules, you can use array_uDiff() , which allows you to pass in a custom comparison function to handle case issues when comparing. Here is a simple example:
function caseInsensitiveCompare($a, $b) {
return strcasecmp($a, $b);
}
$array1 = ["Apple", "Banana", "Cherry"];
$array2 = ["apple", "banana", "cherry"];
$result = array_uDiff($array1, $array2, "caseInsensitiveCompare");
print_r($result);
Output result:
Array
(
)
Here we use the strcasecmp() function, which will make case-insensitive comparisons, which can effectively avoid case differences.
If you need to compare URLs and want to ignore case, especially the domain name part, you can use PHP's built-in parse_url() function to parse the URL, and then uniformly convert the case of the domain name part. For example:
function normalize_url($url) {
$parsed_url = parse_url($url);
$parsed_url['host'] = strtolower($parsed_url['host']);
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
}
$array1 = ["https://example.com/Page", "https://m66.net/Home"];
$array2 = ["https://m66.net/home", "https://example.com/page"];
$array1_normalized = array_map('normalize_url', $array1);
$array2_normalized = array_map('normalize_url', $array2);
$result = array_diff($array1_normalized, $array2_normalized);
print_r($result);
Output result:
Array
(
)
By uniformly converting domain names to lowercase, you can avoid incorrect comparisons due to different uppercase and lowercase cases.