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 Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Inheritance
Mar 22, 2023 programming

PHP Inheritance

Today we are going to talk about Inheritance in PHP. Inheritance is another important topic related to object oriented programming in PHP. It can help you extend the functionalities of a class. ...

8 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
PHP Array Flip
Feb 14, 2023 programming

PHP Array Flip

Today we are going to talk about array flip function in PHP. Array flip is very simple yet useful array function that helps you flip and array. ...

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