How can I determine how many identical elements exist in an array?
$ A = array ('2', '1', '2'); // How to determine which elements are identical and where they are located
Thank you.
Reply to discussion (solution)
You can use array_count_values to obtain the number of identical elements.
$ A = array ('2', '1', '2 ');
Print_r (array_count_values ($ ));
?>
The result is Array ([1] => 1 [2] => 2)
However, to get the same number and want to know the lower mark, the current idea is to loop the array and then compare it.
I only wrote one element with the same statistics. As for the position, I would traverse $ value! The element = 1 is enough.
$array=array("a","b","c","a","c","d","a");function search($ar){$cnt=count($ar);$br=array();for($i=0;$i<$cnt;$i++){$t=$ar[$i];//echo $t;if(!$br[$t]){$br[$t]=1;}else{$br[$t]++;}}return $br;}$cr=search($array);print_r($cr);
Array ([a] => 3 [B] => 1 [c] => 2 [d] => 1)
$a = array('2','1','2');$xt = array_diff(array_count_values($a), array(1));print_r($xt);foreach(array_keys($xt) as $v) { echo $v, ': ', join(',', array_keys(array_filter($a, function($c) use ($v) { return $c == $v; })));}
Array
(
[2] => 2
)
2: 0, 2
Thank you for your help.