How to remove duplicate values from an array in PHP

Sometimes when we work in the project we encountered a situation like where we need to remove the duplicate value from the array. For example, if we need a unique set of the array to run the for each loop to meet some condition in the application. In that situation either we end up in count the same array value twice and not to run the subsequent function when the count value is more than 1.

In the PHP we have array_unique() inbuilt function that provides us the functionality to remove duplicate value from the array. It works on both numeric and string values. You only need to pass the array in the array_unique() function and the function will return the unique value to us.

PHP array_unique() function is easy to use, This function accepts two parameters, one is mandatory and the other is optional. When we have to make array unique we use the array_unique() function. array_unique() PHP function provides an easy way to remove the duplicate entry from an array.

Let’s try to understand the array_unique() function with the help of the below example Check the following code with output:

$array = array("Nike", "Nike", "Mark", "ADIDAS");
// Deleting the duplicate items
$result = array_unique($array);


    [0] => Nike
    [2] => Mark
    [3] => ADIDAS

As you can see we have assigned 4 values in the array. Then we passed that array in array_unique() function, you can see in the output duplicate “Nike” value has been removed and we only got 3 values out of 4.


You can see how easy is to remove duplicate values from an array in PHP using array_unique() function. Hope it helped. If you need further assistance in the post, please comment below I would love to help you.

You can also check the other related queries.

How to Remove Null Values From Multidimensional Array In Php

How to Remove Empty Object From JSON in JavaScript

How to remove empty values from an array in PHP