search
HomeBackend DevelopmentPHP ProblemHow to check if there are duplicates in an array in php

In PHP, checking whether there are duplicate values ​​in an array is a basic task. In this article, we will explore how to check whether there are duplicate values ​​in an array in PHP and provide some solutions.

1. Use PHP built-in functions

PHP has many useful built-in functions that can easily solve array problems in PHP. Here is one of those functions:

array_unique() — Remove duplicates from an array

If your goal is to completely eliminate duplicate values, this method is a very simple one. It returns a new array containing only unique values. If there is a sorting problem, you can use the sort() function to sort.

For example:

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

Output:

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

As shown above, we just passed the previous array into the array_unique() function, and Identical values ​​are deduplicated. But this method is only suitable for completely eliminating duplicate values.

2. Use PHP loops

If you need to find all duplicate values ​​​​in an array, probably the best choice is to use a loop. Here is a way to find duplicate values ​​using a loop in PHP:

$array = array(2, 5, 6, 2, 8, 5, 1);
 
$result_array = array();
foreach ($array as $val) {
    $result_array[$val] = isset($result_array[$val]) ? ++$result_array[$val] : 1;
}
 
foreach ($result_array as $key => $val) {
    if ($val > 1) {
        echo "{$key} 重复了{$val}次 <br>";
    }
}

Output:

2 重复了2次
5 重复了2次

In the above code, we use a loop to iterate through the array and use each element as Key to the result array. If a key is taken over, then it is incremented one key at a time. If any key is repeated many times, then we can pass another loop to output this element along with the number of occurrences.

3. Use PHP count() function

Sometimes, we need to quickly check whether there are duplicate values ​​in an array, and there is no need to remove duplicate values. In this case, we can use PHP’s count() function.

The following is an example of this method:

$array = array(2, 5, 6, 2, 8, 5, 1);
 
if (count($array) === count(array_unique($array))) {
    echo "没有重复值";
} else {
    echo "有重复值";
}

Output:

有重复值

In the above code, we use the array_unique() function to remove duplicates, and then use count( ) function checks whether two arrays are of the same size. If they are the same and there are no duplicate values ​​within the array, then count($array) === count(array_unique($array)) evaluates to true.

Conclusion

Checking whether there are duplicate values ​​in an array can be a common task in PHP. In this article, we have discussed three ways to check whether there are duplicate values ​​in a PHP array:

  1. Duplicate values ​​can be removed from an array using the PHP built-in function array_unique().
  2. Using PHP loop, you can find all duplicate values.
  3. Use the PHP count() function to quickly check whether there are duplicate values ​​in an array.

In addition, similar effects can be achieved using collection methods, just use PHP's built-in SplObjectStorage class. Finally, a piece of code that uses the SplObjectStorage class to check for duplicate values ​​is provided for your reference:

$array = array(2, 5, 6, 2, 8, 5, 1);

$splObj = new SplObjectStorage();

foreach ($array as $value) {
    $splObj->attach($value);
}

if (count($array) === $splObj->count()) {
    echo "没有重复值";
} else {
    echo "有重复值";
}

The above is a method of using PHP to check whether there are duplicate values ​​in an array. I hope it will be helpful to you.

The above is the detailed content of How to check if there are duplicates in 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools