Current Location: Home> Latest Articles> array_diff() and array_filter() jointly implement more complex filtering logic

array_diff() and array_filter() jointly implement more complex filtering logic

M66 2025-05-14

In daily PHP development, filtering arrays is a very common task. Whether it is cleaning up invalid data, removing specific elements, or performing business logic filtering, we may use functions such as array_filter() or array_diff() . Using them alone is already powerful, but when they are combined we can implement more complex and flexible array operations.

This article will use an example to explain how to combine array_diff() and array_filter() to implement complex array filtering logic.

1. Basic review

array_filter()

array_filter() is usually used to filter values ​​in an array that do not meet the criteria based on a callback function:

 $numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, function($n) {
    return $n % 2 === 0;
});
// $even = [1 => 2, 3 => 4]

array_diff()

array_diff() is used to return all values ​​in an array that are not in other arrays:

 $all = ['apple', 'banana', 'cherry'];
$remove = ['banana'];
$result = array_diff($all, $remove);
// $result = [0 => 'apple', 2 => 'cherry']

2. Problem Scenario

Suppose you have an array of data that contains multiple user inputs, which contain some values ​​you want to exclude (such as blacklists, existing items, invalid data, etc.). You hope:

  • Use array_filter() to filter out invalid or incorrectly formatted items;

  • Then use array_diff() to remove those values ​​in the blacklist;

  • Finally, a clean array that can be used for further processing.

3. Practical examples

Suppose we collect a batch of user-submitted URLs, we need:

  1. Remove null values ​​or erroneous formats;

  2. Excluded domain names from the blacklist.

 $userUrls = [
    'https://m66.net/article/123',
    'http://example.com/test',
    '',
    'not_a_url',
    'https://m66.net/login',
    'https://blacklist.com/malicious',
];

$blacklistedDomains = ['blacklist.com'];

// first step:use array_filter() Invalid clearance URL
$validUrls = array_filter($userUrls, function($url) {
    return filter_var($url, FILTER_VALIDATE_URL);
});

// Step 2:Extraction is legal URL Domain name,And filter out the blacklist domain name
$filteredUrls = array_filter($validUrls, function($url) use ($blacklistedDomains) {
    $host = parse_url($url, PHP_URL_HOST);
    return !in_array($host, $blacklistedDomains);
});

// $filteredUrls Now contains legal and not blacklisted URL
print_r($filteredUrls);

The output result is as follows:

 Array
(
    [0] => https://m66.net/article/123
    [1] => http://example.com/test
    [4] => https://m66.net/login
)

Note that we actually used array_filter() twice here, and we can also introduce array_diff() as needed to handle the differences between domain name lists, as shown below:

4. Combined with array_diff() to accurately exclude blacklists

We can first extract all domain names from the URL, and then use array_diff() to exclude blacklist domain names:

 $domains = array_map(function($url) {
    return parse_url($url, PHP_URL_HOST);
}, $validUrls);

$allowedDomains = array_diff($domains, $blacklistedDomains);

// Filter again URL,Only retain belongings allowedDomains of
$finalUrls = array_filter($validUrls, function($url) use ($allowedDomains) {
    $host = parse_url($url, PHP_URL_HOST);
    return in_array($host, $allowedDomains);
});

print_r($finalUrls);

5. Summary

By combining array_filter() and array_diff() , we can handle various complex array filtering logic very flexibly. In actual development:

  • array_filter() is more suitable for checking array elements one by one according to conditions;

  • array_diff() is more suitable for excluding known invalid items or blacklists;

  • Combining the two can not only verify the element content, but also realize set filtering.

Flexible mastering the combination of these functions will significantly improve the efficiency and code readability of your data arrays in PHP.