Home  >  Article  >  Backend Development  >  Can php remove duplicate values ​​from an array?

Can php remove duplicate values ​​from an array?

PHPz
PHPzOriginal
2023-04-27 15:54:30366browse

In the process of writing PHP programs, we often need to perform deduplication operations on arrays. The purpose of deduplication is to avoid interference from duplicate data and to effectively improve program performance. For PHP, removing duplicate values ​​​​from an array is a very basic operation. This article will introduce how to use PHP to achieve this function.

1. Use the array_unique() function

PHP provides a function called array_unique(), which can remove duplicate values ​​from the array and return a new array. The following is the basic syntax of the array_unique() function:

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Parameter description:

  • $array: required parameter, the array from which duplicate values ​​are to be removed.
  • $sort_flags: Optional parameter, used to specify the sorting method of the array. The default is SORT_STRING, which sorts by string type.

The following is an example:

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

The output result is:

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

2. Reset the array using key values

In addition to using array_unique( ) function, we can also remove duplicates through key values ​​and reset the array to a new array. The following is an example:

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

The output result is:

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

3. Use loop traversal

In addition to the above two methods, we can also use loop traversal Perform deduplication. The basic idea of ​​this method is: first create an empty array, and then loop through each element in the original array. If the element is not in the new array, add it to the new array. The following is a sample code:

$arr = array(1, 2, 2, 3, 4, 4, 5);
$new_arr = array();
foreach ($arr as $value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[] = $value;
    }
}
print_r($new_arr);

The output result is:

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

4. Use array_reduce() function

array_reduce() function is used to iteratively reduce array elements into single value. Using this function, we can easily remove duplicate elements from the array. The following is a sample code:

$arr = array(1, 2, 2, 3, 4, 4, 5);
$new_arr = array_reduce($arr, function($carry, $item){
    if(!in_array($item, $carry)){
        $carry[] = $item;
    }
    return $carry;
}, []);
print_r($new_arr);

The output result is:

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

5. Use the array_diff() function

array_diff() function is used to compare the difference sets of arrays. Using this function, we can quickly remove duplicate elements from an array. The following is a sample code:

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

The output result is:

Array
(
    [1] => 2
    [4] => 4
)

6. Use the array_filter() function

array_filter() function is used to filter elements in the array. Using this function, we can quickly remove duplicate elements from an array. The following is a sample code:

$arr = array(1, 2, 2, 3, 4, 4, 5);
$new_arr = array_filter($arr, function($value, $key){
    return !in_array($value, array_slice($arr, 0, $key, true));
}, ARRAY_FILTER_USE_BOTH);
print_r($new_arr);

The output result is:

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

Summary

This article introduces various methods to remove duplicate values ​​​​in arrays in PHP. Using these methods can make We write PHP programs more efficiently. Whether it is using the array_unique() function or resetting the array using key values, or looping through, using the array_reduce() function, array_diff() function or array_filter() function, as long as you master the basic principles and application methods, you can easily Completely implement array deduplication operation.

The above is the detailed content of Can php remove duplicate values ​​from an 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