Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​from an array in php

How to remove duplicate values ​​from an array in php

PHPz
PHPzOriginal
2023-04-25 09:05:06580browse

PHP, as a widely used server-side scripting language, often needs to process arrays. One of the more common problems is how to remove duplicate values ​​​​in the array. This article will introduce several common methods to solve this problem.

1. Use the array_unique function

The array_unique function is PHP's built-in function for removing duplicate values ​​​​in an array. Its use is very simple. You only need to pass it the array that needs to be deduplicated. That’s it. Here is a sample code:

$arr = array(1, 2, 3, 3, 4, 5, 5);
$unique_arr = array_unique($arr);
print_r($unique_arr);

The output of the above code will be:

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

As you can see, the duplicate values ​​in the original array have been removed, leaving only the unique value. It should be noted that the array_unique function does not retain the key-value relationship in the original array by default. Therefore, if you need to retain the key-value relationship, you can re-index the array in combination with the array_values ​​function.

2. Use loop deduplication

Another common deduplication method is to loop through the array. The specific method is to compare each element in the array with the element behind it. If duplicate elements are found, the following elements are deleted through the unset function. The following is a sample code:

$arr = array(1, 2, 3, 3, 4, 5, 5);
for($i = 0; $i < count($arr); $i++)
{
    for($j = $i + 1; $j < count($arr); $j++)
    {
        if($arr[$i] == $arr[$j])
        {
            unset($arr[$j]);
        }
    }
}
print_r($arr);

The output of the above code is the same as using the array_unique function. It should be noted that the method of using loop deduplication is inefficient because it requires multiple loop nestings and the time complexity is O(n²), so it is not recommended for processing large amounts of data.

3. Use the array_flip and array_values ​​functions

Another common deduplication method is to use the value of the array as the key value, and then re-index the key value through the array_values ​​function. The specific method is to first exchange the key values ​​and values ​​​​in the array through the array_flip function, so as to ensure that the values ​​​​in the array are unique, and then re-index the key values ​​​​through the array_values ​​function. The following is a sample code:

$arr = array(1, 2, 3, 3, 4, 5, 5);
$arr = array_flip($arr);
$arr = array_values($arr);
print_r($arr);

The output of the above code is the same as using the array_unique function. It should be noted that since two functions are used to complete the operation, it may not be as efficient as using the array_unique function.

To sum up, the above are several common ways to remove duplicate values ​​from arrays in PHP. Depending on the situation, choosing different methods can improve the efficiency of processing arrays. It should be noted that when processing large amounts of data, more efficient methods should be selected to improve performance.

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