Home >Backend Development >PHP Problem >How to implement array deduplication in php

How to implement array deduplication in php

PHPz
PHPzOriginal
2023-03-29 11:33:501504browse

In web development, array deduplication is a common operation. In PHP, multiple methods are provided to achieve array deduplication. This article will introduce the 3 most commonly used methods.

Method 1: Use the array_unique() function

The array_unique() function can remove duplicate elements from the array and return a new deduplicated array. Using this function is very simple, you just need to pass in the array that needs to be deduplicated as a parameter. The following is a sample code for this function:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 5, 5);
$new_arr = array_unique($arr);
print_r($new_arr);
?>

Output result:

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

This method is very simple, but it cannot retain the key names of the original array. If you need to retain the key names of the original array, you can use the method described below.

Method 2: Use loop traversal

Using PHP's for loop or foreach loop, you can traverse the entire array and remove duplicate elements through judgment. The following is an example code for using a foreach loop to implement array deduplication:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 5, 5);
$new_arr = array();
foreach ($arr as $value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[] = $value;
    }
}
print_r($new_arr);
?>

Output result:

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

This method can retain the key names of the original array, but it requires a loop to traverse the array, so it is efficient relatively low.

Method 3: Use the array_flip() and array_keys() functions

The array_flip() function can reverse the key names and key values ​​of the array. Using this function, you can use the value of the original array as the key name and the key name as the value to generate a new array. Then use the array_keys() function to obtain the key name of the new array, thereby obtaining the deduplicated array. The following is a sample code:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 5, 5);
$tmp_arr = array_flip($arr);
$new_arr = array_keys($tmp_arr);
print_r($new_arr);
?>

Output result:

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

This method can retain the key names of the original array and is more efficient, but it requires the use of two functions, which is more cumbersome.

Summary

In actual development, choose the method to implement array deduplication according to the specific situation. If you do not need to retain the key names of the original array, you can use the simplest array_unique() function; if you need to retain the key names of the original array, you need to use loop traversal or array_flip() and array_keys() functions.

The above is the detailed content of How to implement array deduplication 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