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

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)
Nov 27, 2022 programming

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)

5 commands to interact with and online git repository ...

13 Min Read Read More
PHP Regular Expressions
Mar 22, 2023 programming

PHP Regular Expressions

Today we are going to learn about regular expressions in PHP. A regular expression is a sequence of characters that specifies a search pattern. it's important to know how to apply them in PHP. ...

5 Min Read Read More
PHP Array Diff
Feb 14, 2023 programming

PHP Array Diff

Today we are going to talk about array diff functions in PHP. If you want to find the items in your array that are not present in the other arrays, array diff is your friend. ...

5 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 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 ...