Home  >  Article  >  Backend Development  >  PHP implements array removal of duplicates

PHP implements array removal of duplicates

WBOY
WBOYOriginal
2023-05-05 21:19:07598browse

PHP is an open source scripting language widely used in web programming and is ideal for use with HTML to generate dynamic web pages.

In PHP programming, arrays are often used to store data. However, in actual development, there may be duplicate elements in the array, which will affect the correctness and efficiency of the program. Therefore, achieving array deduplication is an important issue in PHP development.

The following will introduce several methods to achieve array deduplication in PHP.

1. Use a loop

The simplest way is to use a loop to traverse the array, compare each element with the element after it, and delete the element after it if it is duplicated. The complexity of doing this is O(n^2), which is not very efficient.

The code is as follows:

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

for($i = 0; $i < count($arr); $i++){
    for($j = $i + 1; $j < count($arr); $j++){
        if($arr[$i] == $arr[$j]){
            array_splice($arr,$j,1); //删除数组中指定位置的元素
            $j--; //删除元素后,数组长度减1,需要将指针向前移一位
        }
    }
}

print_r($arr); //输出结果为 [1,2,3,4,5]

2. Use array_unique function

PHP provides the built-in function array_unique(), which can directly perform deduplication operations on arrays. This function returns a new array in which each element appears only once.

The code is as follows:

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

$new_arr = array_unique($arr); //去重

print_r($new_arr); //输出结果为 [1,2,3,4,5]

This method is easy to use and relatively efficient, but please note that this function can only remove elements with the same value, not exactly the same elements.

3. Use array_flip and array_keys functions

array_flip() function flips the keys and values ​​in the array and returns a new array, where the value of the original array is the key of the new array, The keys are the values ​​of the new array. Since the keys of the new array are unique, you can directly use the array_keys() function to obtain the keys to achieve deduplication.

The code is as follows:

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

$new_arr = array_flip($arr); //翻转数组的键和值
$new_arr = array_keys($new_arr); //获取新数组的键,即为去重后的结果

print_r($new_arr); //输出结果为 [1,2,3,4,5]

This method is similar to array_unique(). It can only remove elements with the same value, but it is faster and suitable for processing large amounts of data.

4. Use loops and in_array function

While looping through the array, use the in_array() function to determine whether the current element appears among the elements that have been processed. If it does, delete the current element. element.

The code is as follows:

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

$new_arr = array(); //新数组用于存储不重复的元素

foreach($arr as $value){ 
    if(!in_array($value,$new_arr)){ //判断当前元素是否已经存在于新数组中
        $new_arr[] = $value; //若不存在,则添加至新数组
    }
}

print_r($new_arr); //输出结果为 [1,2,3,4,5]

This method is also O(n^2) complexity, but it is more efficient than method 1 and is suitable for processing small and medium-sized data.

To sum up, there are many ways to implement array deduplication in PHP. Which method to choose needs to be chosen based on the actual situation. At the same time, we must pay attention to the efficiency and correctness of the algorithm, as well as the simplicity and readability of the code.

The above is the detailed content of PHP implements array removal of duplicates. 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