Remove Duplicate Element or Value from Array in PHP

Remove Duplicate Element or Value from Array in PHP

Remove Duplicate elements from an array in PHP. Here we will learn how to remove duplicate elements/values from an array without using any function in PHP and also will learn how to remove elements/values from an array using inbuilt function array_unique() in PHP.

There are two way to remove duplicate elements and values from an array in PHP

  • Using array_unique, Remove duplicate elements from array in PHP
  • Remove duplicate elements from array in php without using function

Using array_unique, Remove duplicate elements from array in PHP

PHP function array_unique()

The array_unique() function removes duplicate elements and values from an array.

Syntax

Syntax of array_unique function

array_unique(array, sorttype)

Parameter Of Array_unique() function

ParameterDescription
arrayRequired. Specifying an array
sorttype Optional. Specifies how to compare the array elements/items. Possible values:
1). SORT_STRING – Default. Compare items as strings
2). SORT_REGULAR – Compare items normally (don’t change types)
3). SORT_NUMERIC – Compare items numerically
4). SORT_LOCALE_STRING – Compare items as strings, based on current locale

Example – Using Array_unique() function, remove values or elements from an array.

<?php
$arr = array("a"=>"java","b"=>"php","c"=>"laravel","d"=>"codeigniter","e"=>"java", "f"=>"laravel");
print_r(array_unique($arr));
?>

Remove duplicate elements from an array in PHP without using the function

Here we will take few examples for remove elements from an array without using any PHP functions.

Let’s take the first example. In this example, we have one array and it contains duplicate values or elements.

Here we will use the for each loop and continue the statement of the PHP and remove the duplicate elements from an array.
First of all, we need to create a new array that will store the non-duplicate values of an array. When we iterate the for each loop with duplicate array and inside this loop we will assign value for new array and final the loop is stopped. than we print the new array.

<?php
$arr = array("a"=>"java","b"=>"php","c"=>"laravel","d"=>"codeigniter","e"=>"java", "f"=>"laravel");
$newArr = array();

foreach($arr as $inputArrayItem) {
    foreach($newArr as $outputArrayItem) {
        if($inputArrayItem == $outputArrayItem) {
            continue 2;
        }
    }
    $newArr[] = $inputArrayItem;
}
print_r($newArr);
?>

Now let’s take a second example, here we will remove elements from in array without using PHP function. We will iterate array with foreach loop and also use in_array() for remove duplicate values or elements from in array.

<?php
 $arr = array("a"=>"java","b"=>"php","c"=>"laravel", "d"=>"codeigniter","e"=>"java", "f"=>"laravel");

 $count = count($arr);

 $outputArr = [];

 foreach($arr as $key => $arrVal) {
    if(!in_array($arrVal, $outputArr)){
        array_push($outputArr, $arrVal);
    }
 }
 print_r ($outputArr);
?>

Conclusion

Here you have learned to remove Duplicate elements from an array in PHP without using any function and how to remove elements/values from an array using the PHP inbuilt function array_unique().

Leave a Reply

Your email address will not be published. Required fields are marked *