Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the most repeated values in an array [duplicate]

Tags:

arrays

php

I have an array of numbers like this:

$array = array(1,1,1,4,3,1);

How do I get the count of most repeated value?

like image 667
Robert Wilson Avatar asked Nov 28 '22 09:11

Robert Wilson


1 Answers

This should work:

$count=array_count_values($array);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo "The most occuring value is $keys[0][1] with $keys[0][0] occurences."
like image 116
Connor Peet Avatar answered Dec 22 '22 10:12

Connor Peet