Home PHP How to remove duplicate values in a PHP array?
 
 
 

How to remove duplicate values in a PHP array?

PDF Print E-mail

Question: How to remove duplicate values in a PHP array?

 

Answer:

  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

)

 

 

+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Please input the anti-spam code that you can read in the image.
 
 
Banner