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

How to remove duplicate elements from array in PHP

PHPz
Release: 2023-04-20 11:47:41
Original
508 people have browsed it

In PHP, array is a very powerful data type used to store and operate a set of data. However, sometimes we need to remove duplicate elements from an array. This article will introduce how to remove duplicate elements from an array in PHP.

1. Use the array_unique() function

We can use the array_unique() function in PHP to remove duplicate elements from the array. This function will return a new array containing all the distinct elements in the original array.

The following is a sample code using the array_unique() function:

$numbers = array(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
$unique_numbers = array_unique($numbers);
print_r($unique_numbers);
Copy after login

The above code will output the following:

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

2. Use loops to remove duplicates

In addition to using the array_unique() function, we can also use loops to remove duplicate elements in the array. We can use a foreach loop to iterate through each element in the array and compare it to a new array.

The following is a sample code using loop deduplication:

$numbers = array(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
$unique_numbers = array();
foreach ($numbers as $number) {
    if (!in_array($number, $unique_numbers)) {
        $unique_numbers[] = $number;
    }
}
print_r($unique_numbers);
Copy after login

The above code will output the following:

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

3. Use array_flip() and array_values() functions

Another method is to use the array_flip() and array_values() functions. First, we use the array_flip() function to swap the keys and values ​​in the array. This way, duplicate values ​​are converted into keys. Then, we use the array_values() function to get all the values ​​in the array.

The following is sample code using the array_flip() and array_values() functions:

$numbers = array(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
$unique_numbers = array_values(array_flip($numbers));
print_r($unique_numbers);
Copy after login

The above code will output the following:

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

Summary

In PHP, we can use array_unique() function, loops and array_flip() and array_values() functions to remove duplicate elements from an array. Which method you choose depends on your specific needs and personal preferences. No matter which method we choose, all these methods can help us handle duplicate elements in arrays easily, making our code more concise and efficient.

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