Sling Academy
Home/PHP/3 Ways to remove duplicates from an array in PHP

3 Ways to remove duplicates from an array in PHP

Last updated: January 11, 2024

Introduction

Handling arrays is a key part of programming in PHP, and a common issue that developers often face is removing duplicate values from an array. Whether you are managing data coming from a database, processing user input, or dealing with any list of elements, having a set of unique values can be essential. In this tutorial, we will explore several ways to remove duplicates from an array in PHP to ensure you have the tools needed for dealing with such scenarios in your applications.

Understanding Arrays in PHP

Before diving into the ways to remove duplicates, it is important to understand what arrays are and how they work in PHP. An array in PHP is a data structure that allows storing multiple values in a single variable. It can hold elements of any type, such as integers, strings, or even other arrays, and can be indexed or associative.

Indexed Arrays

Indexed arrays use numeric indexes, starting at 0. For example:

$fruits = ['apple', 'banana', 'apple', 'cherry'];

Associative Arrays

Associative arrays use named keys that you assign to values. For example:

$fruits = ['a' => 'apple', 'b' => 'banana', 'c' => 'apple', 'd' => 'cherry'];

Method 1: Using array_unique()

Perhaps the most straightforward way to remove duplicates from an array in PHP is by using the array_unique() function. This built-in function takes an array and returns a new array without duplicate values.

For example:

$uniqueFruits = array_unique($fruits);

The resulting array, $uniqueFruits, would contain each fruit only once. Note that array_unique() preserves the keys from the original array, which may be important depending on your use case. If you’d like to reindex the keys, you can combine it with array_values():

$uniqueFruits = array_values(array_unique($fruits));

Method 2: Custom Loop Method

If you need more control over the process or simply want to understand how to implement this functionality yourself, you might consider using a custom loop to remove duplicates.

Here’s one way to do it:

$uniqueFruits = [];
foreach ($fruits as $fruit) {
    if (!in_array($fruit, $uniqueFruits)) {
        $uniqueFruits[] = $fruit;
    }
}

This code iterates over the original $fruits array and adds each element to the $uniqueFruits array only if it’s not already in it. Note that this might not be the most efficient method if you’re dealing with a very large array since in_array() itself has to iterate over the $uniqueFruits array to check for the existence of each fruit.

Method 3: Using array_flip()

Another interesting way to remove duplicates from an array relies on the fact that an array cannot have duplicate keys. If you flip an array twice, you can effectively remove duplicate values.

Here’s what that looks like:

$fruitsFlipped = array_flip($fruits);
$uniqueFruits = array_flip($fruitsFlipped);

This works because when you flip the array the first time, the values become keys (hence, removing any duplicates since keys must be unique). When flipped back, the keys (original values) are restored, minus any duplicates that were removed.

Performance Considerations

When removing duplicates from arrays in PHP, it’s important to be aware of the potential performance implications. The array_unique() function is straightforward but may not be the best choice for large arrays. Implementing a custom loop can be more memory-intensive and might slow down your script, especially with a larger dataset. The array flip method offers a good compromise between ease of use and performance.

Conclusion

In this tutorial, we’ve covered several methods for removing duplicates from an array in PHP including using array_unique(), custom loop checks, and utilizing array_flip(). Depending on your application’s needs and performance requirements, you can choose the method that best fits your scenario. Experiment with these techniques and keep an eye on memory usage and execution time to find the perfect balance for your application.

Remember, every situation is unique and may require a different approach for optimal performance and clarity in your code. Happy coding!

Next Article: How to map two arrays into an object in PHP

Previous Article: PHP: How to Count the Occurrences of an Array Value

Series: PHP Data Structure Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array