- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing duplicate elements from an array in PHP
The ‘array_flip’ function can be used, that will reverse the values as indices and keys as values.
<?php $my_arr = array(45, 65, 67, 99, 81, 90, 99, 45, 68); echo "The original array contains "; print_r($my_arr); $my_arr = array_flip($my_arr); $my_arr = array_flip($my_arr); $my_arr= array_values($my_arr); echo " The array after removing duplicate elements is "; print_r($my_arr); ?>
The original array contains Array (  => 45  => 65  => 67  => 99  => 81  => 90  => 99  => 45  => 68 ) The array after removing duplicate elements is Array (  => 45  => 65  => 67  => 99  => 81  => 90  => 68 )
An array is defined and duplicate elements from the array can be found and removed using the ‘array_flip’ function, that basically reverses the keys/index as values and values as keys. This way, the value that is repeated comes twice in the index and one of them is removed since indices have to be unique. Again, the ‘array_flip’ function is used to get the array back to the original form.
- Removing empty array elements in PHP
- How do I recursively remove consecutive duplicate elements from an array?
- PHP program to find missing elements from an array
- Removing an array element from a MongoDB collection
- Adding and Removing Elements in Perl Array
- How to remove duplicate elements of an array in java?
- C Program to delete the duplicate elements in an array
- Removing item from array in MongoDB?