Remove Duplicate Values from Array PHP

[ad_1]

To remove Duplicate elements or values from an array in PHP; Through this tutorial, 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.

How to remove duplicate values from array using PHP

Let’s demonstrate two ways 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

Parameter Description
array Required. 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

That’s all, We 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().

Recommended PHP Tutorials

  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

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00