PHP Remove Duplicate Elements or Values from Array PHP

PHP Remove Duplicate Elements or Values from Array PHP

To remove Duplicate elements or values 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.

Here, we will demonstrate two way to remove duplicate elements and values from an array in PHP

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

Remove duplicate elements from array in PHP Using array_unique

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().

You may like

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. How to Convert String to Array in PHP
  4. Array Push and POP in PHP | PHP Tutorial
  5. PHP Search Multidimensional Array [key and value and return key]
  6. PHP Array to String Conversion – PHP Implode
  7. Array Functions In PHP – PHP Tutorial

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

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