PHP | Remove duplicate elements from Array Last Updated : 30 Mar, 2018 Comments Improve Suggest changes Like Article Like Report You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : array ( [0] => 4 [4] => 2 [5] => 7 [6] => 3 ) In C/Java, we have to traverse the array and for each element you have to check if its duplicate is present. But PHP provides an inbuilt function (array_flip()) with the use of which we can remove duplicate elements without the use of loop. The array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys. Note that the values of array need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result. Note: If a value has several occurrences, the latest key will be used as its value, and all others will be lost. Also as array_flip() returns array with preserved keys, we will use array_values() which will re-order the keys. Approach: The idea is to use this property of the array_flip() function of selecting the latest key as its value if the value has multiple occurrences. We will use the array_flip() function twice to remove the duplicate values. On using the array_flip() function for the first time, it will return an array with keys and values exchanged with removed duplicates. On using it the second time, it will again reorder it to the original configuration. Below is the implementation of above idea: php <?php // define array $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5); // print original array echo "Original Array : \n"; print_r($a); // remove duplicate values by using // flipping keys and values $a = array_flip($a); // restore the array elements by again // flipping keys and values. $a = array_flip($a); // re-order the array keys $a= array_values($a); // print updated array echo "\nUpdated Array : \n "; print_r($a); ?> Output: Original Array : Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 5 [4] => 1 [5] => 3 [6] => 2 [7] => 4 [8] => 5 ) Updated Array : Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 3 [4] => 4 ) Comment More infoAdvertise with us Next Article PHP | Remove duplicate elements from Array A Abhinav96 Follow Improve Article Tags : PHP Web Technologies Similar Reads Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index 4 min read How to Remove Duplicate Elements from an Array using Lodash ? Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un 3 min read Remove duplicate elements from an array using AngularJS We have given an array and the task is to remove/delete the duplicates from the array using AngularJS.Approach: The approach is to use the filter() method and inside the method, the elements that don't repeat themselves will be returned and the duplicates will be returned only once.Hence, a unique a 2 min read Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing 3 min read How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i 4 min read How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i 4 min read PHP | Program to delete an element from array using unset() function Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] => Harsh [1] => Nishant [2] => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); 2 min read How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table 2 min read How to Splice Duplicate Item from JavaScript Array? Given an array of numbers or strings containing some duplicate values, the task is to remove these duplicate values from the array without creating a new array or storing the duplicate values anywhere else.Examples: Input: arr = [1, 2, 3, 4, 3, 2, 1];Output: [1,2,3,4]Input: [1, 4, 6, 1, 2, 5, 2, 1, 4 min read Like