How to use the array_diff() function to generate a comparison report of two arrays?
In PHP, array_diff() is a very practical function that helps you compare two or more arrays and returns elements in the first array but not in other arrays. By using the array_diff() function, you can easily generate an array comparison report to find the differences between arrays.
The array_diff() function is used to calculate the difference between two arrays. It returns a new array containing all elements in the first array but not in other arrays. If you compare two arrays, the function returns an array containing elements that only appear in the first array.
Function prototype:
array_diff(array $array1, array $array2, array ...$arrays): array
parameter:
$array1 : The first array, used to compare with other arrays.
$array2, ...$arrays : One or more arrays, compared to the first array.
Return value: Returns an array containing all elements that appear in the first array but not in other arrays.
Suppose we have two arrays representing item items of two lists, and we want to compare them and generate a report showing items that exist in the first list but not in the second list. Here is a sample code:
<?php
// The first array:Product ListA
$arrayA = array('apple', 'banana', 'orange', 'Grape', 'watermelon');
// The second array:Product ListB
$arrayB = array('apple', 'banana', 'watermelon');
// use array_diff() Calculate the difference set
$difference = array_diff($arrayA, $arrayB);
// Output difference report
echo "In the listA中但不In the listBProducts in:\n";
foreach ($difference as $item) {
echo $item . "\n";
}
?>
Output result:
In the listA中但不In the listBProducts in:
orange
Grape
In the example above, the array_diff() function helps us find items that exist in $arrayA but not in $arrayB (i.e. "orange" and "grape"). This is the basis for using the array_diff() function to generate comparison reports.
array_diff() can not only compare two arrays, but also multiple arrays. For example, if you have multiple lists of items and want to find items in the first list that differ from all other lists, array_diff() can also help you easily.
<?php
// Product ListA
$arrayA = array('apple', 'banana', 'orange', 'Grape', 'watermelon');
// Product ListB
$arrayB = array('apple', 'banana', 'watermelon');
// Product ListC
$arrayC = array('apple', 'orange', 'watermelon');
// use array_diff() Comparison of three arrays
$difference = array_diff($arrayA, $arrayB, $arrayC);
// Output difference report
echo "In the listA中但不In the listBand listCProducts in:\n";
foreach ($difference as $item) {
echo $item . "\n";
}
?>
Output result:
In the listA中但不In the listBand listCProducts in:
Grape
By comparing multiple arrays, array_diff() returns items (i.e. "grape") that exists in $arrayA but not in $arrayB and $arrayC .
The array_diff() function is a very powerful tool that can help you compare two or more arrays and find the differences. array_diff() can provide great help whether it is data comparison, report generation, or array element deduplication. In this way, you can quickly identify which elements differ between different arrays, thus better analyzing the data.