Home >Backend Development >PHP Problem >How to remove duplicate data from an array in php

How to remove duplicate data from an array in php

PHPz
PHPzOriginal
2023-04-23 17:49:25642browse

In actual development, we often need to clear duplicate data from an array. In PHP, removing duplicate data is an easy task because PHP provides many built-in functions to complete this task.

The following are some PHP built-in functions that can be used to remove duplicate data in arrays:

  1. array_unique() function

array_unique() can Used to perform deduplication operations on arrays. It returns a new array containing all the distinct values ​​in the original array.

Usage:

$array = array("a","b","c","d","a","b","e");
$new_array = array_unique($array);
print_r($new_array);

Output:

Array(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [6] => e
)
  1. array_diff() function

array_diff() function can be used to compare two array and returns a new array containing distinct elements.

Usage:

$array1 = array("a","b","c","d","a","b","e");
$array2 = array("c", "d", "e", "f");
$new_array = array_diff($array1, $array2);
print_r($new_array);

Output:

Array(
    [0] => a
    [1] => b
)
  1. array_merge() function

array_merge() function can be used to merge multiple arrays are combined into one array. If there are duplicate data after merging, the last occurring value will be retained.

Usage:

$array1 = array("a","b","c","d","a","b","e");
$array2 = array("c", "d", "e", "f");
$new_array = array_merge($array1, $array2);
print_r($new_array);

Output:

Array(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => a
    [5] => b
    [6] => e
    [7] => c
    [8] => d
    [9] => e
    [10] => f
)
  1. array_intersect() function

array_intersect() function can be used to compare two arrays and returns a new array containing elements present in both arrays.

Usage:

$array1 = array("a","b","c","d","a","b","e");
$array2 = array("c", "d", "e", "f");
$new_array = array_intersect($array1, $array2);
print_r($new_array);

Output:

Array(
    [2] => c
    [3] => d
    [6] => e
)

If you want to remove all duplicate data in the array, you can use the above functions in combination. The specific method is as follows:

$array1 = array("a","b","c","d","a","b","e");
$array2 = array("c", "d", "e", "f");
$new_array = array_merge(array_diff($array1, array_intersect($array1, $array2)), array_diff($array2, array_intersect($array1, $array2)));
print_r($new_array);

Output:

Array(
    [0] => a
    [1] => b
    [4] => f
)

The above method can clear all duplicate data in the array and retain all different elements. If you need to keep the first or last one in the duplicate data, you can use the following method:

Keep the first one in the duplicate data:

$new_array = array_unique($array1);

Keep the last one in the duplicate data:

$new_array = array_intersect($array1, array_unique(array_reverse($array1)));

The above is the detailed content of How to remove duplicate data from an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn