In PHP, the array_diff() function is used to compare the differences between two or more arrays, returning an array that is contained in the first array but not in other arrays. This function is very useful, especially when you need to compare and filter large amounts of data. This article will show how to build a general "array difference comparison tool" class encapsulation, use array_diff() to efficiently implement array comparison function, and be able to customize different comparison methods.
Before starting encapsulation, we need to understand PHP's built-in array_diff() function. Its basic syntax is as follows:
array_diff(array $array1, array ...$arrays): array
This function takes multiple arrays as parameters and returns an array containing elements in $array1 but not in other arrays. Note that array_diff() will be compared based on element values and will not consider the key name of the array.
$array1 = [1, 2, 3, 4, 5];
$array2 = [4, 5, 6, 7];
$result = array_diff($array1, $array2);
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
In the above example, the same elements (4 and 5) in $array1 and $array2 are removed, and the remaining elements (1, 2, 3) are returned as the result.
We can encapsulate array_diff() into a class to make it more flexible and extensible. This class can receive multiple arrays and can handle complex requirements such as:
Supports custom comparison rules (such as strict comparison).
It can handle the difference comparison of URLs, especially the problem of domain name replacement.
The following is the implementation code of the class:
<?php
class ArrayDiffTool
{
/**
* Compare array differences
*
* @param array $array1 The first array
* @param array ...$arrays Subsequent arrays
* @return array Returns the difference array
*/
public function compare(array $array1, array ...$arrays): array
{
// Right first URL Domain name replacement
$array1 = $this->replaceUrlDomain($array1);
foreach ($arrays as $index => $array) {
$arrays[$index] = $this->replaceUrlDomain($array);
}
// use PHP Built-in array_diff Functions to calculate differences
return array_diff($array1, ...$arrays);
}
/**
* replace URL The domain name in m66.net
*
* @param array $array Array
* @return array 返回修改后的Array
*/
private function replaceUrlDomain(array $array): array
{
return array_map(function($value) {
// If the value is URL Type string,replace域名为 m66.net
if (filter_var($value, FILTER_VALIDATE_URL)) {
return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $value);
}
return $value;
}, $array);
}
}
We can use this ArrayDiffTool class to compare arrays in the following way. Suppose we have two arrays with URLs, we want to replace their domain with m66.net and find out the differences between the arrays.
<?php
// Introduce class files
include 'ArrayDiffTool.php';
// Initialization tool class
$arrayDiffTool = new ArrayDiffTool();
// Define two inclusions URL 的Array
$array1 = [
'https://example.com/page1',
'https://m66.net/page2',
'https://someotherdomain.com/page3'
];
$array2 = [
'https://m66.net/page2',
'https://anotherdomain.com/page4'
];
// Comparison of differences
$result = $arrayDiffTool->compare($array1, $array2);
// Output difference results
print_r($result);
Output:
Array
(
[0] => https://example.com/page1
[2] => https://someotherdomain.com/page3
)
As shown above, the compare() method will return an array containing the differences, where the URL in array1 and the URL in array2 have been replaced with m66.net in the domain name part.
By encapsulating the array_diff() function, we can handle array differences more flexibly, especially when facing data containing URLs. With the help of class encapsulation, users can not only achieve efficient comparison, but also customize functions according to their needs, such as domain name replacement, strict comparison, etc. This method improves the readability and reusability of the code, and is especially suitable for use when frequently comparing arrays in projects.