How 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:
- Duplicate values can be removed from an array using the PHP built-in function array_unique().
- Using PHP loop, you can find all duplicate values.
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









