In PHP, the array_diff() function is used to compare two or more arrays, returning an array containing all elements that exist in the first array but not in the other arrays. This function is very useful when you need to remove all elements that exist in another array from one array.
The basic syntax of the array_diff() function is as follows:
array_diff(array $array1, array ...$arrays): array
$array1 : The first array to be compared with other arrays.
$arrays : One or more arrays that compare with $array1 .
This function returns an array containing all elements in $array1 that do not exist in other arrays.
Here is a simple example showing how to use array_diff() to compare two arrays and return elements that exist in the first array but not in the second array:
<?php
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(4, 5, 6, 7, 8);
$result = array_diff($array1, $array2);
print_r($result);
?>
Array
(
[0] => 1
[1] => 2
[2] => 3
)
In the above example, array_diff() returns an element that exists in the array $array1 but does not exist in $array2 . That is, elements 1, 2, 3 are the only elements in $array1 , while 4, 5 are overwritten by $array2 .
Suppose you have an array containing user information, and you need to exclude some existing users from it. You can use array_diff() to achieve this:
<?php
$userList = array("Alice", "Bob", "Charlie", "David");
$blockedUsers = array("Charlie", "David");
$activeUsers = array_diff($userList, $blockedUsers);
print_r($activeUsers);
?>
Array
(
[0] => Alice
[1] => Bob
)
In this example, only unblocked users are retained in the $activeUsers array, and Charlie and David are excluded.
If you have multiple arrays to compare, you can pass multiple arrays to the array_diff() function, and the result will be the only element in all arrays that exists in the first array:
<?php
$array1 = array("apple", "banana", "cherry");
$array2 = array("banana", "cherry", "date");
$array3 = array("cherry", "date", "elderberry");
$result = array_diff($array1, $array2, $array3);
print_r($result);
?>
Array
(
[0] => apple
)
In this example, "apple" only appears in $array1 , while other elements "banana" , "cherry" , "date" exist in other arrays, so these elements are excluded.
Suppose you are developing a system where users upload content contains multiple URLs, and you want to exclude links to certain domain names from it. You can help you achieve this through array_diff() :
<?php
$urls = array("https://www.example.com", "https://m66.net/about", "https://www.example.com/contact", "https://m66.net/home");
$excludeUrls = array("https://www.example.com");
$filteredUrls = array_diff($urls, $excludeUrls);
print_r($filteredUrls);
?>
Array
(
[1] => https://m66.net/about
[3] => https://m66.net/home
)
In this example, array_diff() will exclude all links with domain name www.example.com and only retain links with m66.net domain names.
array_diff() is a very practical array comparison function in PHP. Whether it is used to filter unnecessary data, compare multiple arrays, or apply it to exclude specific content in actual projects, this function can help you process array data efficiently. Mastering its basic usage and application skills will make you more comfortable in development.