Share via:

Removing Duplicates From PHP Arrays

To get rid of duplicated entries in arrays we may either create our own function, or we can use the build-in array_unique(); function, both of which is demonstrated in this tutorial.


By. Jacob

Edited: 2018-01-26 12:33

Removing duplicates from arrays in PHP.

Arrays are interesting to work with, because they allow us to do some really cool stuff in our code. I am still learning new things about arrays, and I have been coding PHP for the last 10+ years. Sometimes I still find myself facing some really simple problems, and just not knowing how to solve them in the most efficient way. However, I have come to learn that efficiency really should not be our first focus when coding.

Based on our individual experience and skill level, I think we should always aim to do things the best way we know. But, mostly it is just about getting something to work, and getting working code out there.

I recently faced a problem where I had to remove duplicates from an array, and I ended up using a combination of isset() and array keys. This method of finding duplicates is generally very fast, and keys allows you to use all kinds of characters, which means you do not even have to use hashed key names. For a long time, I would first hash the values I wanted to use as keys, until I recently learned that this was not necessary. Apparently any binary string will work as an array key. Armed with this knowledge, we can easily determine if something already exists in an array.

function a_unique($array) {
  $array_unique = array();
  foreach ($array as $key => $value) {
    $array_unique["$value"] = true;
  return array_keys($array_unique);
$array_made_unique = a_unique($array);


Why the problem exists

Before I dive into solving this problem in PHP, I just want to get one thing out of the way. When looking at a problem where you have duplicated entries in an array, it is usually because a function gave you duplicated results, possible from a database query. This is where you should first look if you can optimize your query, in any way, to give you a more specific result. Solving the problem in PHP can unnecessarily complicate matters, and even slow down your script. Not by much. But, it is not just about speed, it is also about making things easier. I am sure there are also examples were using an SQL query is slower than PHP, but it is often easier, and that is what I am mainly looking for now.

A SQL query that would get you a results without duplicates could make use of SELECT DISTINCT, the full query can look like this:

SELECT DISTINCT ip_address FROM request_log_table;

The above assumes that you are using a database based log solution. Basically, it can be used to count the number of unique ip addresses that has visited your page. It is much easier that doing a basic SELECT, and solving the problem in PHP.

You can even count the result directly in the query, so you will not have to do much additional coding:

SELECT COUNT (DISTINCT ip_address) FROM request_log_table;

If you were to do the same with PHP, you would end up with additional complexity to handle something your database does out-of-the-box. This something I have sometimes been guilty of, so some of my older code is really dirty and unnecessarily complex.

Removing duplicates with PHP

There are also cases where your only option is to use PHP for the job. Personally, I have found that array keys can sometimes be used very effectively to solve the problem in cases where you are using a loop to work with the array data.

I have really come to like the simplicity of the foreach loop. I know that for and while loop constructs are faster, but in this case I prefer simplicity. Let us look at a solution to the problem below.

function a_unique($array) {
  $array_unique = array();
  foreach ($array as $key => $value) {
    $array_unique["$value"] = true;
  return array_keys($array_unique);

Now, we just have to call the a_unique(); function when we want to remove duplicated values from an array.

$array_duplicates_removed = a_unique($some_array);

foreach ($array_duplicates_removed as &$value) {
  echo $value . '<br>';

PHP also has its own build-in function to remove duplicates, the array_unique() function.

  $input = array("a" => "green", "red", "b" => "green", "blue", "red");
  $result = array_unique($input);


  1. PHP: array_unique –