How to remove duplicate values in a PHP array

H

How Can We Help?

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

)

About the author

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.

About Author

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.

Follow Me