Home > Backend Development > PHP Problem > How to check if there are duplicates in an array in php

How to check if there are duplicates in an array in php

PHPz
Release: 2023-04-26 13:37:35
Original
900 people have browsed it

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);
Copy after login

Output:

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

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>";
    }
}
Copy after login

Output:

2 重复了2次
5 重复了2次
Copy after login

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 "有重复值";
}
Copy after login

Output:

有重复值
Copy after login

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 "有重复值";
}
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template