Remove Duplicate Values from Array PHP

Remove Duplicate Values from Array PHP

To remove Duplicate elements or values from an array in PHP; Through this tutorial, we will learn how to remove duplicate elements/values from an array without using any function in PHP and also will learn how to remove elements/values from an array using inbuilt function array_unique() and array_flip() in PHP.

How to remove duplicate values from an array in PHP

If you’re working with arrays in PHP, you may find that there are duplicate values that you want to remove. Fortunately, PHP provides several built-in functions for accomplishing this task. Here, you will explore various ways to remove duplicate values from an array in PHP.

  • Method 1: Using array_unique()
  • Method 2: Using a loop
  • Method 3: Using array_flip() and array_keys()

Method 1: Using array_unique()

The simplest way to remove duplicate values from an array in PHP is to use the built-in function array_unique(). This function takes an array as input and returns a new array with all duplicate values removed. Here’s an example:

<?php
$array = array(1, 2, 2, 3, 3, 3);
$unique_array = array_unique($array);
print_r($unique_array);
?>

The given code is written in PHP, and it creates an array named $array with the values 1, 2, 2, 3, 3, 3.

Then, the code uses the PHP built-in function array_unique() to remove any duplicate values from the array. The unique values are then stored in a new array named $unique_array.

Finally, the print_r() function is used to display the contents of the $unique_array.

So, the output of this code will be:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
)

Here, the duplicate values of 2 and 3 have been removed from the original array and only unique values 1, 2, and 3 are displayed in the $unique_array.

Method 2: Using a loop

Another way to remove duplicate values from an array is to use a loop. We can iterate over the array and check if each value is already present in a new array. If the value is not present, we add it to the new array. Here’s an example:

<?php
$array = array(1, 2, 2, 3, 3, 3);
$unique_array = array();
foreach($array as $value) {
    if(!in_array($value, $unique_array)) {
        $unique_array[] = $value;
    }
}
print_r($unique_array);
?>

This is a PHP code that creates an array $array containing some integer values, and then creates an empty array $unique_array. The code then iterates over each value in $array using a foreach loop, and for each value, it checks if it already exists in the $unique_array using the in_array() function.

If the value does not exist in the $unique_array, it is added to the $unique_array using the array push function $unique_array[] = $value;. The if statement with the ! operator ensures that only unique values are added to the $unique_array.

Finally, the code prints the $unique_array using the print_r() function, which outputs the contents of the array to the screen. The resulting output will be an array with only the unique values from the original array in the same order as they appeared in the original array. In this case, the output will be:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
)

Method 3: Using array_flip() and array_keys()

Another way to remove duplicate values from an array is to use the array_flip() and array_keys() functions. We can flip the array, so that the values become keys, and then use the array_keys() function to get the keys. This will give us a new array with all duplicate values removed. Here’s an example:

<?php
$array = array(1, 2, 2, 3, 3, 3);
$flipped_array = array_flip($array);
$unique_array = array_keys($flipped_array);
print_r($unique_array);
?>

The code defines an array of integers, $array, with six elements. The values in the array are 1, 2, 2, 3, 3, 3.

Next, the array_flip function is used to flip the keys and values of $array. This means that each value in $array becomes a key in the $flipped_array, and each key in $array becomes a value in $flipped_array. Since keys in an array must be unique, this effectively removes any duplicate values from $array.

After flipping the array, the array_keys function is used to extract the keys (which are now unique values) from $flipped_array and store them in $unique_array.

Finally, the print_r function is used to print the contents of $unique_array to the screen. The output of the script will be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

This shows that the original array $array contained three unique values: 1, 2, and 3.

Conclusion

In this article, we have explored various ways to remove duplicate values from an array in PHP. The simplest way is to use the array_unique() function, but there are other ways to achieve the same result. Depending on your requirements, you can choose the most suitable method. Removing duplicate values from an array is an important task when working with data, and PHP provides us with several ways to accomplish this.

Recommended PHP Tutorials

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. How to Convert String to Array in PHP
  4. Array Push and POP in PHP | PHP Tutorial
  5. PHP Search Multidimensional Array [key and value and return key]
  6. PHP Array to String Conversion – PHP Implode
  7. Array Functions In PHP – PHP Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *