Verification: a143cc29221c9be0

Php array remove duplicates from array

Remove Duplicate Values From An Array In PHP

In this example, we are taking a simple array as an example and the array_unique() method helps to remove the duplicate value in an array. so you can see the below example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

$arr = array(1, 2, 2, 3, 4);

$final_arr = array_unique($arr);

echo "

"
;

print_r($final_arr);

echo "

";

//output

Array

(

    [0] => 1

    [1] => 2

    [3] => 3

    [4] => 4

)

?>

Remove duplicate values from associative array in PHP

In this example, we are taking an associative array as an example and the array_unique() method helps to remove the duplicate value in an array. here in this example car values are duplicates so the array_unique() method will remove the duplicate car value. so you can see the below example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

$arr=array("a"=>"apple","b"=>"bike","c"=>"car","d"=>"dog","f"=>"car");

$final_arr = array_unique($arr);

echo "

"
;

print_r($final_arr);

echo "

";

//output

Array

(

    [a] => apple

    [b] => bike

    [c] => car

    [d] => dog

)

?>

Topic

Remove Duplicates From Multi Dimensional Array?

View Content

Doing an API call and want to remove duplicates so I can display the products by brand. Problem I am having now is when I try to display the brands, I am getting duplicate brands. When I display the brands I am getting Nike, Nike, Reebook. When I just want, Nike, Reebok.

My Code
Code: [Select]
$results = array(); //array from API call

$results2 = $results['data'];

function multi_unique($array) {
        foreach ($array as $k=>$na)
            $new[$k] = serialize($na);
        $uniq = array_unique($new);
        foreach($uniq as $k=>$ser)
            $new1[$k] = unserialize($ser);
        return ($new1);
    }

$results3 = multi_unique($results2);

foreach ($results3 as $var)
{
echo $var['brand'];
?>


}

?>
The array
Code: [Select]
{
   "errors": [
   ],
   "warnings": [
   ],
   "data": [
      {
         "url": "http://[...]",
         "image_url": "[...]",
         "image_thumb_url": "[...]",
         "keyword": "Red Running Shoes",
         "description": "Red Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": null,
         "currency": "USD",
         "merchant": "",
         "brand": "Nike",
         "upc": "0944588778",
         "isbn": null,
         "sales": 23
      },
      {
         "url": "http://[...]",
         "image_url": "http://[...]",
         "image_thumb_url": null,
         "keyword": "Blue Running Shoes",
         "description": "Blue Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": "49.99",
         "currency": "USD",
         "merchant": "",
         "brand": "Nike",
         "upc": "0944588779",
         "isbn": null,
         "sales": 12
      }
{
         "url": "http://[...]",
         "image_url": "http://[...]",
         "image_thumb_url": null,
         "keyword": "Black Running Shoes",
         "description": "Black Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": "49.99",
         "currency": "USD",
         "merchant": "",
         "brand": "Reebok",
         "upc": "0944554779",
         "isbn": null,
         "sales": 12
      }
   ],
   "coupons": [
         
   ],
   "countryCode": "US",
   "page": 1,
   "limit": 10,
   "totalRecords": 3
}


Use the array_unique() Function to Remove Duplicate Values From an Array in PHP

We can use the array_unique() function to remove duplicate values from an array. The array_unique() function is a specialized function for removing the duplicate values from an array. The correct syntax to use this function is as follows.

array_unique($array, $flags);

The array_unique() function has two parameters. The details of its parameters are as follows.

Variables Description
$array It is the array from which we want to remove the duplicate values.
$flags It specifies the sorting pattern for the array. The sorting flags are of five types.
SORT_REGULAR compares items normally
SORT_NUMERIC compares items numerically
SORT_STRING compares items as strings
SORT_LOCALE_STRING compares items as strings, based on the current locale.

This function returns the array free of duplicate values. The program below shows the ways by which we can use the array_unique() function to remove duplicate values from an array in PHP.

Output:

Array before removal: 
array(11) {
  [0]=>
  string(4) "Rose"
  [1]=>
  string(4) "Lili"
  [2]=>
  string(7) "Jasmine"
  [3]=>
  string(8) "Hibiscus"
  [4]=>
  string(8) "Daffodil"
  [5]=>
  string(5) "Daisy"
  [6]=>
  string(8) "Daffodil"
  [7]=>
  string(5) "Daisy"
  [8]=>
  string(4) "Lili"
  [9]=>
  string(7) "Jasmine"
  [10]=>
  string(7) "Jasmine"
}
Array after removal: 
array(6) {
  [0]=>
  string(4) "Rose"
  [1]=>
  string(4) "Lili"
  [2]=>
  string(7) "Jasmine"
  [3]=>
  string(8) "Hibiscus"
  [4]=>
  string(8) "Daffodil"
  [5]=>
  string(5) "Daisy"
}

The function has returned the filtered array.

Now, if we pass the $flags parameter, the output will be changed.

Output:

Array before removal: 
array(11) {
  [0]=>
  string(4) "Rose"
  [1]=>
  string(4) "Lili"
  [2]=>
  string(7) "Jasmine"
  [3]=>
  string(8) "Hibiscus"
  [4]=>
  string(8) "Daffodil"
  [5]=>
  string(5) "Daisy"
  [6]=>
  string(8) "Daffodil"
  [7]=>
  string(5) "Daisy"
  [8]=>
  string(4) "Lili"
  [9]=>
  string(7) "Jasmine"
  [10]=>
  string(7) "Jasmine"
}
Array after removal: 
array(1) {
  [0]=>
  string(4) "Rose"
}

The function has now sorted the array numerically.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.