How to Remove duplicate element from an array with out using built-in function in PHP

Suppose $inputArray is our array which have some repeated elements.

$inputArray = array(1, 4, 2, 1, 6, 4, 9, 7, 2, 9);
$outputArray = array();


foreach($inputArray as $inputArrayItem){
    foreach($outputArray as $outputArrayItem) {
        if($inputArrayItem == $outputArrayItem) {
           continue 2;
        }
    }

    $outputArray[] = $inputArrayItem;
}


echo "<pre>";
print_r($outputArray);


Output : 

Array
(
    [0] => 1
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 9
    [5] => 7
)


You like this post don't forgot to leave a comment.

Chears :)
Happy coding..

Post a Comment

Previous Post Next Post