How to determine how many duplicate values ​​there are in an array in php

青灯夜游
Release: 2023-03-14 22:42:01
Original
3445 people have browsed it

Method: 1. Use the "$arr2=array_count_values($arr)" statement to count the number of occurrences of all values ​​in the array; 2. Use the foreach statement to traverse the "$arr2" array to determine whether the number of occurrences of the array value is Greater than 1; 3. Count the number of times greater than 1 to know the number of repeated values ​​in the array.

How to determine how many duplicate values ​​there are in an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php judgment array How many duplicate values ​​are there

In php, the array_count_values() function can be used to determine how many duplicate values ​​there are in the array

array_count_values() function is used Count the number of occurrences of all values ​​in an array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("A","Cat","Dog","A","Dog",1,2,3,3,2,1);
echo "原数组:";
var_dump($arr);
echo "数组各值的出现次数:";
$arr2=array_count_values($arr);
var_dump($arr2);
?>
Copy after login

How to determine how many duplicate values ​​there are in an array in php

It can be seen that the array_count_values() function will return an associative array, the key name of its element is the value of the original array, and the key value is the value that appears in the original array frequency.

In the number of occurrences, any value greater than 1 is a duplicate value. We only need to count the number.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("A","Cat","Dog","A","Dog",1,2,3,3,2,1);
$arr2=array_count_values($arr);
$count = 0;
foreach($arr2 as $value){
	if($value>1){
		$count ++;
	}
}
echo "数组中的重复值有:".$count;
?>
Copy after login

How to determine how many duplicate values ​​there are in an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine how many duplicate values ​​there are in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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