During development, we often encounter situations where we need to compare two or more arrays, especially to find elements unique to a specific array. PHP provides a very useful built-in function — array_diff() — that can easily accomplish this task.
array_diff() is a PHP function used to compare differences between arrays. It returns an array containing only the elements that appear in the first array, excluding any values that exist in the subsequent arrays.
array array_diff(array $array1, array ...$arrays)
$array1 is the main array you want to compare.
$arrays are one or more arrays you want to compare against the main array.
Suppose we have recorded the user's original interests and a new list of interests submitted by the user, and we want to find out which ones are newly added.
<?php
$originalInterests = ['Music', 'Movies', 'Travel'];
$newInterests = ['Music', 'Movies', 'Travel', 'Photography', 'Reading'];
<p>$addedInterests = array_diff($newInterests, $originalInterests);</p>
<p>print_r($addedInterests);<br>
?><br>
Output:
Array
(
[3] => Photography
[4] => Reading
)
As you can see, array_diff() returns elements that only exist in $newInterests, which are the interests newly added by the user.
Suppose you are handling a list of websites submitted by a user and want to filter out URLs that the user has already submitted, leaving only the new ones.
<?php
$existingUrls = [
'https://m66.net/article/123',
'https://m66.net/blog/2024',
];
<p>$submittedUrls = [<br>
'<a rel="noopener" target="_new" class="" href="https://m66.net/article/123">https://m66.net/article/123</a>',<br>
'<a rel="noopener" target="_new" class="" href="https://m66.net/tools/checker">https://m66.net/tools/checker</a>',<br>
'<a rel="noopener" target="_new" class="" href="https://m66.net/blog/2024">https://m66.net/blog/2024</a>',<br>
'<a rel="noopener" target="_new" class="" href="https://m66.net/contact">https://m66.net/contact</a>',<br>
];</p>
<p>$newUrls = array_diff($submittedUrls, $existingUrls);</p>
<p>print_r($newUrls);<br>
?><br>
Output:
Array
(
[1] => https://m66.net/tools/checker
[3] => https://m66.net/contact
)
The result shows that these two URLs submitted by the user are new and were not present in the original list.
array_diff() compares arrays by value and uses non-strict comparison (does not compare data types), so values like '1' and 1 are considered equal.
If you need strict comparison (including data types), you can use array_diff_assoc() or array_diff_key().
The returned result preserves the original array keys; if you don't need the keys, you can re-index using array_values().
$cleanList = array_values(array_diff($newUrls, $existingUrls));
array_diff() is a very powerful tool suitable for data filtering, log comparison, user behavior analysis, and many other scenarios. Once you understand how it works, many array processing tasks become simple and efficient.
I hope this article helps you better understand and use array_diff()! If you encounter similar problems in development, give this function a try—it might save you a lot of code.