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
Release: 2023-04-23 17:51:53
Original
646 people have browsed it

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);
Copy after login

Output:

Array(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [6] => e
)
Copy after login
  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);
Copy after login

Output:

Array(
    [0] => a
    [1] => b
)
Copy after login
  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);
Copy after login

Output:

Array(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => a
    [5] => b
    [6] => e
    [7] => c
    [8] => d
    [9] => e
    [10] => f
)
Copy after login
  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);
Copy after login

Output:

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

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);
Copy after login

Output:

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

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);
Copy after login

Keep the last one in the duplicate data:

$new_array = array_intersect($array1, array_unique(array_reverse($array1)));
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template