In PHP, array_diff() and array_diff_assoc() are functions used to compare arrays and return different elements. Although they have similar functions, there are some differences in details. This article will explore the main differences between these two functions and analyze their respective applicable scenarios.
The array_diff() function is used to calculate the difference between two or more arrays, returning an array that is included in the first array but not in other arrays. It should be noted that array_diff() will only compare the values of the array, but will not compare the key names.
grammar:
array_diff(array $array1, array $array2, array ...$arrays): array
$array1 : The first array to be compared with other arrays.
$array2, ...$arrays : Other arrays to be compared with the first array.
Sample code:
<?php
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow");
$result = array_diff($array1, $array2);
print_r($result);
?>
Output:
Array
(
[b] => green
[c] => blue
)
In this example, array_diff() only compares the values of arrays $array1 and $array2 , returning elements in $array1 that do not appear in $array2 .
The array_diff_assoc() function is similar to array_diff() , but it not only compares the values of an array, but also compares the key names. Therefore, elements are considered different only when the value and key names are different.
grammar:
array_diff_assoc(array $array1, array $array2, array ...$arrays): array
$array1 : The first array to be compared with other arrays.
$array2, ...$arrays : Other arrays to be compared with the first array.
Sample code:
<?php
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
Output:
Array
(
[b] => green
[c] => blue
)
In this example, array_diff_assoc() also returns the element in $array1 , but it compares the key name and the key value. Since $array1 and $array2 differ in the combination of key names and key values (such as the value of key names b is different), the returned result is consistent with the output of array_diff() .
characteristic | array_diff() | array_diff_assoc() |
---|---|---|
Comparative elements | Compare values only | Compare values and key names |
Does the key name affect the result | Will not affect the results | Key names will also affect the results |
Applicable scenarios | Only care about the comparison of values | Pay attention to the comparison of values and key names, and require that the key names and values of elements match exactly |
Scenarios using array_diff() :
When you only care about the values of the array, and not the key names, you can use array_diff() . For example, find out which elements in an array are not in another array, regardless of the key names of those elements.
Scenarios using array_diff_assoc() :
When you not only care about the values, but also need to consider the key names, you should use array_diff_assoc() . For example, when comparing two arrays with associated key values, make sure that the value and key name are consistent.
array_diff() and array_diff_assoc() are both very useful array comparison functions. Their main difference lies in whether the key name of the array is considered. When selecting these two functions, you should decide whether you need to consider the key name according to your needs.
If there is a domain name in the code that uses URLs, I will replace its domain name with m66.net . For example:
$url = "https://www.example.com";
The modified code is as follows:
$url = "https://m66.net";