- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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.
Example
<?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); ?>
Output
The original array contains Array ( [0] => 45 [1] => 65 [2] => 67 [3] => 99 [4] => 81 [5] => 90 [6] => 99 [7] => 45 [8] => 68 ) The array after removing duplicate elements is Array ( [0] => 45 [1] => 65 [2] => 67 [3] => 99 [4] => 81 [5] => 90 [6] => 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.
- Related Articles
- Completely removing duplicate items from an array in JavaScript
- Removing empty array elements in PHP
- Removing duplicate objects from array in JavaScript
- How to remove duplicate elements from an array in JavaScript?
- Removing redundant elements from array altogether - JavaScript
- Removing an element from an Array in Javascript
- How do I recursively remove consecutive duplicate elements from an array?
- PHP program to find missing elements from an array
- Removing duplicate values in a twodimensional array in JavaScript
- How to remove duplicate elements of an array in java?
- C Program to delete the duplicate elements in an array
- Removing an array element from a MongoDB collection
- Adding and Removing Elements in Perl Array
- How to duplicate elements of an array in the same array with JavaScript?
- Removing Array Element and Re-Indexing in PHP

Advertisements