There are several ways to deduplicate arrays in php

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-07 11:38:05
Original
2440 people have browsed it

There are three common methods for deduplicating arrays in php: 1. The "array_unique()" function can remove duplicate values ​​​​in the array and return a new array; 2. Use a loop to delete duplicates, use for Or a foreach loop traverses the entire array to determine whether there are duplicates and delete them; 3. Use the "array_flip()" and "array_keys()" functions. The "array_flip()" function exchanges the keys and values ​​of the array, and "array_keys()" Get all key names.

There are several ways to deduplicate arrays in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

There are three common ways to remove duplicates from arrays in php:

1. Use the array_unique() function

array_unique() function to remove duplicates from the array of repeated values ​​and returns a new array.

$array = array("a" => "apple", "b" => "banana", "c" => "apple");
$unique_array = array_unique($array);
print_r($unique_array);
Copy after login

The output result is:

Array
(
    [a] => apple
    [b] => banana
)
Copy after login
Copy after login
Copy after login

2. Use a loop to delete duplicate items

Use a for or foreach loop to traverse the entire array and judge Check if there are duplicates and delete them. This method works well for small arrays, but can be time-consuming for large arrays.

$array = array("a" => "apple", "b" => "banana", "c" => "apple");
foreach ($array as $key => $value) {
  $count = 0;
  foreach ($array as $k => $v) {
      if ($v == $value) {
          $count++;
          if ($count > 1) {
              unset($array[$k]);
          }
      }
  }
}
print_r($array);
Copy after login

Output:

Array
(
    [a] => apple
    [b] => banana
)
Copy after login
Copy after login
Copy after login

3. Use the array_flip() and array_keys() functions

Use the array_flip() function to convert the keys and values ​​of the array Swap, and use array_keys() to get all key names. Since the keys are unique, this effectively creates a set of results that does not contain duplicate items.

$array = array("a" => "apple", "b" => "banana", "c" => "apple");
$unique_array = array_flip(array_flip($array));
print_r($unique_array);
Copy after login

The output is:

Array
(
    [a] => apple
    [b] => banana
)
Copy after login
Copy after login
Copy after login

These methods are only a few methods that can achieve array deduplication. There are other methods such as using array_count_values(), array_reduce(), etc. For developers to master according to actual needs.

The above is the detailed content of There are several ways to deduplicate arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template