• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Remove Duplicates from Array in php with array_unique()

Remove Duplicates from Array in php with array_unique()

March 24, 2022 Leave a Comment

In php, to remove duplicates from an array, the easiest way is to use the array_unique() function to get the unique values and then use array_values() to reindex the array.

$example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6);

print_r(array_values(array_unique($example)));

//Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 7
    [4] => 5
    [5] => 3
    [6] => 6
)

You can also use array_flip() twice and remove duplicates since an array cannot have duplicate keys.

$example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6);

print_r(array_values(array_flip(array_flip($example))));

//Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 7
    [4] => 5
    [5] => 3
    [6] => 6
)

When working with arrays and collections of data in php, it is useful to be able to easily change and manipulate our data structures easily.

One such operation is to be able to get the unique values from an array and remove all duplicate items.

We can easily remove duplicates from a php array and get the unique values with the array_unique() function.

Below is how to use the array_unique() function to remove duplicates from an array. After using array_unique(), we can use array_values() to reindex the array.

$example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6);

print_r(array_values(array_unique($example)));

//Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 7
    [4] => 5
    [5] => 3
    [6] => 6
)

Using array_flip() to Remove Duplicates from Array in php

Another way you can remove all of the duplicates from a php array is with the array_flip() function.

The array_flip() function flips an array’s keys and values.

We can take advantage of the fact that an array cannot have duplicate keys and call array_flip() twice.

Below is how to use the array_flip() function to remove duplicates from an array. After using array_flip() twice, we can use array_values() to reindex the array.

$example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6);

print_r(array_values(array_flip(array_flip($example))));

//Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 7
    [4] => 5
    [5] => 3
    [6] => 6
)

Hopefully this article has been useful for you to learn how to remove duplicates from arrays in php.

Other Articles You'll Also Like:

  • 1.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 2.  php array_map() – Create New Array from Callback Function
  • 3.  php range() – Create Array of Elements Within Given Range
  • 4.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 5.  How to Get Session ID in php with session_id()
  • 6.  Get Array Length with php count() Function
  • 7.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 8.  How to Create an Empty Array in php
  • 9.  php Move File from One Directory to Another Folder
  • 10.  Remove First Character from String Using php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy