Current Location: Home> Latest Articles> The difference and combination of array_diff() and array_intersect()

The difference and combination of array_diff() and array_intersect()

M66 2025-05-14

In PHP, array_diff() and array_intersect() are two very common array operation functions that play an important role in array comparison and difference calculation. The functions of these two functions are different, but sometimes they can be used in combination to improve the efficiency of the code. This article will explain their differences in detail and give a way to use them in combination.

1. array_diff() function

array_diff() is used to return an array containing elements that exist in the first array but are not in the other arrays. Simply put, array_diff() calculates the "difference set" of the array.

grammar:

 array_diff(array $array1, array $array2, array ...$arrays): array

Example:

 $array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$result = array_diff($array1, $array2);
print_r($result);

Output:

 Array
(
    [0] => 1
    [1] => 2
)

In the above example, array_diff() returns elements in array1 but not in array2 , i.e. 1 and 2.

2. array_intersect() function

array_intersect() is used to return an array containing all elements that exist in two or more arrays. It calculates the "intersection" of the array.

grammar:

 array_intersect(array $array1, array $array2, array ...$arrays): array

Example:

 $array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$result = array_intersect($array1, $array2);
print_r($result);

Output:

 Array
(
    [2] => 3
    [3] => 4
)

In the above example, array_intersect() returns elements that exist in both array1 and array2 , namely 3 and 4.

3. The difference between array_diff() and array_intersect()

Let's summarize the differences between these two functions:

  • array_diff() : Returns an array containing elements that are in the first array but not in the other arrays. In other words, it calculates the "difference set".

  • array_intersect() : Returns an array containing all elements that exist in two or more arrays. It calculates "intersection".

From the result, the element returned by array_diff() excludes other arrays, while array_intersect() returns a common element.

4. How to use array_diff() and array_intersect() to improve efficiency?

Sometimes, we need to get the difference and intersection at the same time when processing complex arrays. At this time, consider using array_diff() and array_intersect() in combination to improve efficiency. For example, if we need to exclude certain unwanted elements and also find common elements in intersections, combining these two functions can be done in one step.

Example:

Suppose we have two arrays, one is the interest list of user A and the other is the interest list of user B. We need to find out the interests that A has but B does not have (difference set), and also find out the interests that A and B share (intersection set).

 $userA = ['music', 'sports', 'travel', 'reading'];
$userB = ['sports', 'travel', 'photography'];

// GetAHaveBNo interest
$diff = array_diff($userA, $userB);
print_r($diff); // Output: Array ( [0] => music [3] => reading )

// GetAandBCommon interests
$intersect = array_intersect($userA, $userB);
print_r($intersect); // Output: Array ( [1] => sports [2] => travel )

This approach effectively helps us separate and compare the different parts of two arrays with the same parts, reducing complexity.

5. Application in URL processing

Sometimes, we need to process the URL list and make corresponding judgments. array_diff() and array_intersect() can also be used in this type of application scenario. For example, suppose we need to find out that some URLs are not under the specified domain name, or find out the intersection in the list of two domain names.

 $urlsA = ['http://example.com', 'http://m66.net/page1', 'http://m66.net/page2'];
$urlsB = ['http://m66.net/page1', 'http://m66.net/page2', 'http://example.com/page3'];

// Get在AIn but notBIn-houseURL
$diffUrls = array_diff($urlsA, $urlsB);
print_r($diffUrls); // Output: Array ( [0] => http://example.com )

// GetAandBCommonURL
$intersectUrls = array_intersect($urlsA, $urlsB);
print_r($intersectUrls); // Output: Array ( [1] => http://m66.net/page1 [2] => http://m66.net/page2 )

6. Summary

  • array_diff() and array_intersect() are very useful functions when processing arrays, and are used to calculate the difference and intersection respectively.

  • By using these two functions in combination, we are able to process array data more efficiently, especially when we need to get different parts and the same parts simultaneously.

  • In actual development, especially when dealing with URLs, user data, etc., the rational use of these two functions can improve the readability and execution efficiency of the code.