Home  >  Article  >  Backend Development  >  php remove duplicates from array

php remove duplicates from array

WBOY
WBOYOriginal
2023-05-07 21:28:37551browse

PHP is a very popular programming language that many developers refer to as "server-side Javascript". The use of PHP is very common when developing web applications. Arrays are a very common data type in PHP. However, many times we need to remove duplicate elements from an array in order to better manipulate the data. Well, in this article, we will explore how to remove duplicate elements from an array using PHP.

First, let us look at an array containing repeated elements:

$my_array = array(1, 2, 3, 4, 5, 6, 1, 2, 3, 7, 8, 9);

This array contains repeated elements 1, 2, and 3. Now we want to remove duplicate elements from this array. PHP provides several different ways to achieve this goal.

Method 1: Use array_unique function

PHP provides a built-in function array_unique(), which can easily remove duplicate elements from an array. This function accepts an array parameter and returns a new array containing all unique elements from the original array.

Here's how to use the array_unique() function to remove duplicate elements from an array:

$unique_array = array_unique($my_array);

The function will return the following results:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [9] => 7
    [10] => 8
    [11] => 9
)

As shown above, the new array $unique_array contains all unique elements in the original array $my_array. Duplicate elements 1, 2, 3 have been removed.

Method 2: Use array_flip and array_keys functions

The idea of ​​this method is to use PHP's built-in function array_flip() for key-value exchange, using the value of the original array as the key name, ensuring that no Repeatability, then use the array_keys() function to extract the key names as elements of the new array.

Here is sample code using this approach:

$no_duplicate_array = array_keys(array_flip($my_array));

The function will return the following results:

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

As shown above, the new array $no_duplicate_array contains the original array$ All unique elements in my_array. Duplicate elements 1, 2, 3 have been removed.

Method 3: Using loops and in_array function

This method uses a for loop to traverse the original array and store the value into the new array, and then use the in_array function to check whether it already exists in the new array The element, if present, is not added, otherwise the element is added.

Here is sample code using this approach:

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

The function will return the following results:

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

As shown above, the new array $unique_array contains the original array$ All unique elements in my_array. Duplicate elements 1, 2, 3 have been removed.

It should be noted that each method has its own advantages and limitations. Using the array_unique() function is the simplest and most efficient, but may cause some performance issues. Using the array_flip and array_keys functions is a clever way to interchange keys and values, and works well for large arrays. Manual methods of looping through and inspecting elements provide more flexibility and more direct control over the deduplication process, but can result in bloated and inefficient code.

Finally, we need to choose different methods to remove duplicate elements in the array according to different situations. No matter which method we choose to use, we should always keep our code simple and efficient. This way you can write better PHP code.

The above is the detailed content of php remove duplicates from array. 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