Current Location: Home> Latest Articles> array_filter() and array_unique() to deduplicate effective elements

array_filter() and array_unique() to deduplicate effective elements

M66 2025-06-04

In PHP, we often need to deduplicate the array or filter invalid elements. To accomplish these tasks, PHP provides many built-in functions, where array_filter() and array_unique() are two very commonly used functions. Today we will explore how to combine these two functions to deduplicate effective elements in an array.

What are array_filter() and array_unique() ?

  • array_filter() : This function is used to filter elements in an array. It will iterate over each element in the array and decide whether to retain this element based on the given callback function (callback). If the callback function returns true , the element will be preserved; otherwise, it will be discarded.

  • array_unique() : This function is used to remove duplicate elements in an array. It checks the elements in the array and removes all duplicate values, keeping only the first-time occurrence of elements.

Combining array_filter() and array_unique() for deduplication

In some cases, we may need to filter out invalid or unwanted elements first, and then deduplicate the array. To achieve this, we can use array_filter() and array_unique() together.

Code example:

Suppose we have an array containing strings and numbers, we want to filter out null and invalid values ​​first, and then deduplicate valid elements.

 <?php
// Original array,Includes null values、Repeat elements and invalid values
$array = [1, 2, 3, '', null, 4, 4, 'apple', '', 'banana', 'apple', 'm66.net', 'apple'];

// use array_filter() Filter out invalid elements
$filteredArray = array_filter($array, function($value) {
    // Filter out empty values、nulland other invalid values
    return !empty($value);
});

// use array_unique() Remove duplicate valid elements
$uniqueArray = array_unique($filteredArray);

// Output result
print_r($uniqueArray);
?>

Code parsing:

  1. array_filter() : We use the callback function function($value) to filter the array. !empty($value) ensures that we only retain non-null and non- null elements. empty() will consider empty strings, 0 , null and other null values ​​to be invalid elements, so it filters out those elements.

  2. array_unique() : Deduplicate the filtered array, and only the value of each element appears for the first time.

Running results:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [5] => 4
    [6] => apple
    [7] => banana
    [10] => m66.net
)

Combined with application scenarios

Suppose that when we process data obtained from an API (such as http://api.m66.net/data ), the array may contain invalid data items (such as empty strings, duplicate elements, etc.), and we only want to get valid and unique elements. At this time, using array_filter() and array_unique() can help us clean up data quickly.

For example, suppose we obtain an array of user's shopping cart information from the m66.net API interface, we can filter out invalid items through the following code and deduplicate:

 <?php
// Simulation from API Getting the shopping cart array
$cartItems = ['apple', '', 'banana', 'apple', 'm66.net', null, 'banana', 'm66.net', 'apple'];

// Filter out invalid items
$validItems = array_filter($cartItems, function($item) {
    return !empty($item);
});

// Go to the heavy
$uniqueItems = array_unique($validItems);

print_r($uniqueItems);
?>

The end result will be a shopping cart list that removes empty values, duplicates and invalid items.

Summarize

By combining array_filter() and array_unique() , we can flexibly filter invalid elements in an array and deduplicate them. The combination of these two is very practical in actual development, especially when dealing with external data sources (such as APIs), which can help us ensure the validity and uniqueness of the data.