Home > Backend Development > PHP Tutorial > How to deduplicate and remove duplicate elements in PHP array

How to deduplicate and remove duplicate elements in PHP array

王林
Release: 2023-09-05 15:50:01
Original
1479 people have browsed it

如何在 PHP 数组中去重和删除重复的元素

How to deduplicate and delete duplicate elements in a PHP array

In PHP development, when it comes to array operations, sometimes it is necessary to remove duplicates in the array element. This article explains how to use different methods to deduplicate and remove duplicate elements, and provides code examples.

Method 1: Use the array_unique() function to remove duplicates

PHP provides the array_unique() function to remove duplicate elements from the array. This function returns a new array containing the contents of the original array after removing duplicate elements.

Sample code:

<?php
$array = array('apple', 'banana', 'orange', 'apple', 'grape', 'banana');

$result = array_unique($array);

print_r($result);
?>
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [4] => grape
)
Copy after login

Method 2: Use a loop to traverse the array to remove duplicates

In addition to using the array_unique() function, Deduplication can be performed by looping through the array. The specific steps are as follows:

  1. Create a new empty array to store the deduplicated elements;
  2. Traverse each element in the original array;
  3. Determine whether the current element already exists in the new array, and if it does not exist, add it to the new array;
  4. The final new array is the result after deduplication.

Sample code:

<?php
$array = array('apple', 'banana', 'orange', 'apple', 'grape', 'banana');

$newArray = array();

foreach($array as $value) {
    if(!in_array($value, $newArray)) {
        $newArray[] = $value;
    }
}

print_r($newArray);
?>
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login

Method 3: Use array_flip() function to remove duplicates

In some cases , if the elements in the array are all unique, you can use the array_flip() function to swap the keys and values ​​to achieve the effect of deduplication. It should be noted that using this method will delete the duplicate keys in the key-value pair, and only the first duplicate value will be retained.

Sample code:

<?php
$array = array('apple', 'banana', 'orange', 'apple', 'grape', 'banana');

$newArray = array_flip($array);

print_r($newArray);
?>
Copy after login

Output result:

Array
(
    [apple] => 0
    [banana] => 1
    [orange] => 2
    [grape] => 4
)
Copy after login

Method 4: Use the array_values() function to re-index the array

If you want the array after deduplication The index increases continuously starting from 0, and the array_values() function can be used to re-index the array.

Sample code:

<?php
$array = array('apple', 'banana', 'orange', 'apple', 'grape', 'banana');

$result = array_unique($array);

$newArray = array_values($result);

print_r($newArray);
?>
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login

Summary

In PHP development, deduplication and deletion of duplicate elements are common operational requirements. This article describes how to achieve duplication using different methods such as the array_unique() function, looping through the array, array_flip() function, and array_values() function, and provides corresponding code examples. Developers can choose the appropriate method to operate the array according to the actual situation.

The above is the detailed content of How to deduplicate and remove duplicate elements in PHP array. 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