Remove Duplicate Elements from array in php Without using Function

During development we met with arrays containing occurrence of the same values multiple times which we don't need for further processing. Those duplicate values arrays can be built from 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 to learn about removing multiple occurrence of same values from an array so let’s begin and go through in following sections:

1. How to remove duplicate (multiple occurrence) values from an array in php without using inbuilt function?

Let’s 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 )
 

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