Remove Duplicate Elements from array in php Without using Function
During development we met with arrays containing the occurrence of the same values multiple times which we don't need for further processing. Those duplicate value arrays can be built from a database query, file read operations, or any kind of algorithm.
We can deal with database queries to avoid duplication by applying database query/table unique mechanisms as applicable to avoid duplications.
In the current article we are going to learn about how to remove duplicate elements from arrays in PHP without using built-in functions.
There is a way to achieve unique values using the built in function array_unique
. It is recommended to use built in functions rather than user defined functions until any specific changes are required.
As we are here to learn about removing multiple occurrences of the same values from an array so let's begin and go through in following sections:
Scroll for More Useful Information and Relevant FAQs
- How to remove duplicate (multiple occurrence) values from an array in php without using inbuilt function?
- How to remove duplicate values from an Associative array in php?
- Filter duplicate values in Key-Value manner
- Find duplicate values in array without using a function
- Feedback: Your input is valuable to us. Please provide feedback on this article.
How to remove duplicate (multiple occurrence) values from an array in php without using inbuilt function?
Lets see with below program for demonstration.
$givenArray = array(2,5,2,10,4,5,8,16);
$uniqueArry = array();
foreach($givenArray as $val) { //Loop1
foreach($uniqueArry as $uniqueValue) { //Loop2
if($val == $uniqueValue) {
continue 2; // Referring Outer loop (Loop 1)
}
}
$uniqueArry[] = $val;
}
print_r($uniqueArry);
Output:
Array ( [0] => 2 [1] => 5 [2] => 10 [3] => 4 [4] => 8 [5] => 16 )
Explanation:
We have a Numeric array $givenArray. Outer loop (Loop 1) is used to iterate all values of givenArray. Inner loop (Loop 2) is an array used to store unique values only.
Therefore for each iteration of the outer loop (Loop 1) it loops through all elements for uniqueArry until the current assigned element ($val) of outer loop (Loop 1) matches with any of the inner loop (Loop 2) elements.
Once any of the inner array values ($uniqueValue) met the condition ($val == $uniqueValue)
with the current iteration element from the outer loop, it skips the current loop and continues to the next iteration of the outer loop. Here Continue 2 represents outlet loop.
Note: Continue 1 (or can use continue) referred current loop.
We can use the same code logic above for arrays of strings. For example
$givenArray = array("John","David","John","Peter", "Kevin", "Camren","Peter","David");
$uniqueArry = array();
foreach($givenArray as $val) { //Loop1
foreach($uniqueArry as $uniqueValue) { //Loop2
if($val == $uniqueValue) {
continue 2;
}
}
$uniqueArry[] = $val;
}
print_r($uniqueArry);
Output:
Array ( [0] => John [1] => David [2] => Peter [3] => Kevin [4] => Camren )
How to remove duplicate values from an Associative array in php?
Let's understands with below program
$students = array("J"=>"John","D" => "David", "J"=> "John","P" =>"Peter", "K"=> "Kevin", "C"=>"Camren","P"=> "Peter","D"=>"David");
$uniqueArry = array();
foreach($students as $startWith => $student) { //Loop1
foreach($uniqueArry as $uniqueValue) { //Loop2
if($student == $uniqueValue) {
continue 2;
}
}
$uniqueArry[$startWith] = $student;
}
print_r($uniqueArry);
Output:
Array ( [J] => John [D] => David [P] => Peter [K] => Kevin [C] => Camren )
Explanation:
We have given an associative array of students with a list of students name as value and their start name letter as key.
In each iteration current key and value will be assigned to $startWith & $student. Will follow the same logic behaviour as described above.
Here unique array is used unique student name with respected name startWith letter.
Filter duplicate values in Key-Value manner
In the previous method, we learned about removing duplicates using multiple for loops.
Here we will learn to filter duplicate values by storing given array elements as keys without using PHP inbuilt function.
Let's learn how with the following example code.
$givenArray = array(2,5,2,10,4,5,8,16);
$uniqueArry = array();
foreach($givenArray as $val) {
$uniqueArry[$val] = $val;
}
print_r($uniqueArry);
Output:
Array ( [2] => 2 [5] => 5 [10] => 10 [4] => 4 [8] => 8 [16] => 16 )
Code explanation:
- Looping through a given array of elements containing duplicate values
- Declared uniqueArry
- Traversing the array using a for a loop. During iterating over array elements, the value of the current element is assigned to key & values for the new array defined as uniqueArry.
- It does not allow the storage of duplicate keys in arrays that help to avoid multiple occurrences of elements and keep only one value.
Find duplicate values in array without using a function
In the previous example, we learned how to filter duplicates values and generate an array of unique values from a given array.
Now we are going to find duplicates only elements from the given array.
Lets see below code example
$givenArray = array(16, 2,5,2,10,4,5,8,16);
$duplicateValues = array();
$uniqueValues = array();
foreach($givenArray as $val) {
if (!isset( $uniqueValues[$val])) {
$uniqueValues[$val] = $val;
} else {
$duplicateValues[] = $val;
}
}
print_r($duplicateValues);
Result: Array ( [0] => 2 [1] => 5 [2] => 16 )
Code Explanation:
- Define Empty array $duplicateValues & $uniqueValues
- Loop through given array of elements
- Check if $uniqueValues contains the same key as iterative elements. Store element in duplicateValues array if the key exists otherwise store in uniqueValues array.