Select Page

How to remove duplicate values in a PHP array

  1. Use the array_unique function
    • array_unique (PHP 4 >= 4.0.1, PHP 5)
    • array_unique (Takes an input array and returns a new array without duplicate values)
    • array_unique (array $array [, int $sort_flags=SORT_STRING ] )

Example:

CODE:

<?php

//Array with duplicate values
$array = array(“test”,”test1″,”test”,”sum”,”minus”,”max”,”max”,”max”,”min”);

//Removing duplicate values
$newArray = array_unique($array);

echo ‘<pre>’;
print_r($newArray);

?> 

OUTPUT:

//The above code will produce the following results without the duplicate values
Array
(

[0] => test
[1] => test1
[3] => sum
[4] => minus
[5] => max
[8] => min

)

Last Updated On March 22, 2018

1 Comment

  1. Vikas Tyagi

    it worked for me. thanks for nice article.

    Reply

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.