Free cookie consent management tool by TermsFeed Generator PHP Array Unique | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Unique

PHP Array Unique

Last Updated on Feb 14, 2023

Array Unique

Array unique removes the duplicate values from an array. In case of a duplication it keeps the first value and removes the rest.

array_unique($array,FLAG);

The first argument is the array

And the second argument is a flag to tell php how to compare those values

$array = ['10',10,'pratham','amir','pratham'];
print_r(array_unique($array));
// Array ( [0] => 10 [2] => pratham [3] => amir )

As you can see it has compared '10' and 10 and kept one of them. So let’s see if we can do something to keep both of them because their type is not the same.

Flags

We can do that with the help of the second argument. There are flags:

  • SORT_STRING this is the default behavior and compares all the items as string
  • SORT_REGULAR this compares the values without changing their types
  • SORT_NUMERIC this compares the values as numbers
  • SORT_LOCALE_STRING compares items as strings, based on the current locale.

Now all we have to do is to add SORT_REGULAR as the second argument to keep both of the '10' and 10 because their type was not the same. One is string and one is int.

$array = ['10',10,'pratham','amir','pratham'];
print_r(array_unique($array, SORT_REGULAR));
// Array ( [0] => 10 [1] => 10 [2] => pratham [3] => amir )

Conclusion

Now you know about array unique function in PHP.

I recommend you to open a PHP files and try to define an array with duplicated values. then try to remove the duplicates with array unique function.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • remove duplicated values from array with array unique
  • flags for array unique function

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP isset and empty
Mar 21, 2023 programming

PHP isset and empty

Today we are going to learn about isset and empty in PHP. isset and empty are two functions that help us check the existence of a value. They are way more useful than you think. ...

5 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

10 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

10 Min Read Read More
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...