PHP

How to remove duplicate values in a PHP array

Written by Ian Carnaghan · 20 sec read >
  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
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Vikas Tyagi Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Vikas Tyagi
Guest

it worked for me. thanks for nice article.