How to remove duplicates from php array? Brief analysis of three methods

PHPz
Release: 2023-04-12 10:40:05
Original
5605 people have browsed it

When developing web applications, it is often necessary to use arrays to store data. Sometimes we need to deduplicate these arrays for subsequent processing and analysis. This article will introduce several array deduplication methods in PHP.

Method 1: Use the array_unique function

PHP provides a built-in function array_unique to remove duplicate values ​​​​in the array. The usage is very simple, you just need to pass in the array to be deduplicated.

Sample code:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 5);
$result = array_unique($arr);
print_r($result);
?>
Copy after login

Output result:

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

Method 2: Use loop traversal

Another deduplication method is to use a loop to traverse the array, Just delete duplicate values ​​when encountered.

Sample code:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 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);
        }
    }
}
print_r($arr);
?>
Copy after login

Output result:

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

Method 3: Use array_flip and array_keys functions

This method uses PHP array keys that cannot be repeated Features, deduplication can be achieved by flipping the keys and values ​​of the array, and then using the array_keys function to return the key value.

Sample code:

<?php
$arr = array(1, 2, 2, 3, 4, 4, 5);
$temp = array_flip($arr);
$result = array_keys($temp);
print_r($result);
?>
Copy after login

Output result:

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

Summary

This article introduces three commonly used array deduplication methods in PHP, using array_unique function, looping through and using array_flip and array_keys functions. Different methods can be chosen for different scenarios and needs. Which method to choose depends on the actual situation, and developers need to make a choice based on business needs and performance requirements.

The above is the detailed content of How to remove duplicates from php array? Brief analysis of three methods. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!