How to remove duplicate values from an array in PHP

Today, we’ll explain to you how to remove duplicate values from an array in PHP. In this short article, we will look at different types of an array with examples.

Using php array_unique() function we can easily remove duplicate values and get a unique array.

Remove Duplicate Values from:

  1. Numerical Array
  2. Associative Array
  3. Remove by Key name from Associative Array

Let’s take an example for better understanding.

1. Numerical Array

Here, we’ll take a numerical array with duplicate numbers and use the array_unique() function to remove duplicate values from an array.

You can see in the above output array which is not reindexed after removing the duplicate element from an array. So we can use the array_values() functions to get a re-indexed array.

2. Associative Array

Now, we will take a student array which has the student name and marks. The array_unique() function will remove those elements which have the same student name and marks.

3. Remove by Key name from Associative Array

Here, we’ll create a custom function to remove duplicate values by key name from the associative array.

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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