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

How to remove duplicate fields from an array in php

PHPz
PHPzOriginal
2023-04-18 14:08:28577browse

In PHP, array is one of the commonly used data structures. For some application scenarios that need to process data, it is often necessary to remove duplicate fields in arrays. Here are some common methods and techniques.

1. Use the array_unique function

The array_unique function is one of PHP's built-in functions that can remove duplicate values ​​​​from an array. Its use is very simple:

$array = array('a', 'b', 'c', 'b', 'a');
$result = array_unique($array);
print_r($result);

The result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

2. Use array_flip and array_keys functions to achieve deduplication

array_flip function can convert the keys and values ​​​​in the array interchangeable, and the array_keys function can return all key values ​​​​in the array. Therefore, we can combine these two functions to achieve array deduplication:

$array = array('a', 'b', 'c', 'b', 'a');
$result = array_flip(array_flip($array));
print_r($result);

The result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

3. Use foreach loop to deduplicate

Use foreach loop to traverse the array , and use the uniqueness of the array subscript to achieve deduplication:

$array = array('a', 'b', 'c', 'b', 'a');
$temp = array();
foreach ($array as $value) {
  $temp[$value] = true;
}
$result = array_keys($temp);
print_r($result);

The result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

4. Use the in_array function to determine duplication

When using a foreach loop to traverse the array, We can combine the in_array function to determine whether the current value already exists in the result array to achieve deduplication:

$array = array('a', 'b', 'c', 'b', 'a');
$temp = array();
foreach ($array as $value) {
  if (!in_array($value, $temp)) {
    $temp[] = $value;
  }
}
print_r($temp);

The result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

5. Use the array_reduce function to achieve deduplication

array_reduce function can iterate and accumulate the elements in the array. We can combine it with the in_array function to achieve array deduplication:

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

The result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

The above is the removal of arrays in PHP There are several methods for repeating fields. Different methods have their own advantages and disadvantages, and you can choose to use them according to the actual situation.

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