Home > Backend Development > PHP Problem > How to remove duplicate elements from an array in php

How to remove duplicate elements from an array in php

PHPz
Release: 2023-04-27 10:00:28
Original
496 people have browsed it

In PHP, an array is a frequently used data structure that can be used to store multiple values. In actual development, we often need to operate on arrays, such as removing duplicate elements from arrays. This article will introduce several methods to achieve this function.

  1. Use the array_unique function

The array_unique function in PHP can remove duplicate elements from the array and return the processed array. Here is an example:

$array = array('a', 'b', 'c', 'a', 'd', 'b');
$new_array = array_unique($array);
print_r($new_array);
Copy after login

The above code will output the following:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [4] => d
)
Copy after login
Copy after login
Copy after login

Note: Using this method will rearrange the array and eliminate the original key values. If you need to retain the original array index, you can use the following method.

  1. Use foreach loop to traverse

Use foreach loop to traverse the array to determine whether an element already exists, and if it does not exist, add a new array. The following is sample code:

$array = array('a', 'b', 'c', 'a', 'd', 'b');
$new_array = array();

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

print_r($new_array);
Copy after login

The above code will output the following:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [4] => d
)
Copy after login
Copy after login
Copy after login

This method does not rearrange the array and can retain the original index.

  1. Use the array_flip and array_keys functions

Use the array_flip function to use the array value as the key and the key as the value, and then use the array_keys function to get the key value to remove duplicates element. The following is the sample code:

$array = array('a', 'b', 'c', 'a', 'd', 'b');
$new_array = array_keys(array_flip($array));
print_r($new_array);
Copy after login

The above code will output the following:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Copy after login

This method also does not rearrange the array and can retain the original index.

  1. Use the array_reduce function

Use the array_reduce function to reduce the array to a single value by passing all the values ​​in the array to the callback function one by one. You can use the return value of the callback function as a new array and remove duplicates. The following is the sample code:

$array = array('a', 'b', 'c', 'a', 'd', 'b');
$new_array = array_reduce($array, function(&$result, $item) {
    if(!in_array($item, $result)) {
        $result[] = $item;
    }
    return $result;
}, array());

print_r($new_array);
Copy after login

The above code will output the following:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [4] => d
)
Copy after login
Copy after login
Copy after login

This method also does not rearrange the array and can retain the original index.

Summary

The above describes four methods for removing duplicate elements from an array, namely using the array_unique function, using the foreach loop traversal, using the array_flip and array_keys functions, and using the array_reduce function. The specific method to choose can be decided according to the application scenario and actual needs. Different methods have different advantages and disadvantages.

The above is the detailed content of How to remove duplicate elements from an array in php. 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