Home >Backend Development >PHP Problem >Two ways to remove duplicate data from arrays in PHP

Two ways to remove duplicate data from arrays in PHP

PHPz
PHPzOriginal
2023-04-03 11:15:20667browse

When performing PHP data processing, we often encounter such a problem: there is an array that stores data, but there are duplicate elements in the array, and the duplicate elements need to be removed. So how to solve this problem?

Method 1: Use PHP’s own function array_unique

PHP provides a very convenient function array_unique, which can realize the function of deduplication of arrays.

The usage is as follows:

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

where $array is the array that needs to be deduplicated, $sort_flags is an optional parameter, indicating the sorting method.

The principle of this function is: create a new empty array, traverse the elements in the original array, if the element does not exist in the new array, add the element to the new array.

For example:

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

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [7] => 5
)

As you can see, the duplicate elements in the original array have been removed.

Method 2: Traverse the array

In addition to using the functions that come with PHP, you can also achieve deduplication by traversing the array.

The specific method is: create a new empty array, traverse the elements in the original array, if the element does not exist in the new array, add the element to the new array.

The code is implemented as follows:

$arr = array(1, 2, 2, 3, 4, 4, 4, 5);
$new_arr = array(); // 新数组
foreach ($arr as $value) {
    if (!in_array($value, $new_arr)) { // $value 在 $new_arr 中不存在
        $new_arr[] = $value; // 添加到 $new_arr 中
    }
}
print_r($new_arr); // 输出新数组

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [7] => 5
)

Compared with method one, this method is relatively more troublesome, but knowing the underlying principles is very important for understanding PHP arrays. Internal mechanisms are helpful.

It should be noted that the efficiency of method two is relatively low, because it needs to be judged whether the element exists in the new array during traversal. If the original array has more elements, the running time may be longer.

To sum up, there are two main ways to deduplicate PHP arrays: using PHP’s built-in function array_unique and traversing the array. Both methods can achieve the deduplication function, among which using PHP’s built-in function array_unique is more effective. Simple and fast.

The above is the detailed content of Two ways to remove duplicate data from arrays 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