You can count the number of occurrences of each value with array_count_values .
$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
var_dump($counts);
/*
array(5) {
["test"]=>
int(2)
["cat"]=>
int(2)
["this"]=>
int(1)
["that"]=>
int(1)
["then"]=>
int(1)
}
*/
To get the most common value, you can call max in the array and then access the first value with array_search .
$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
$max = max($counts);
$top = array_search($max, $counts);
var_dump($max, $top);
, :
$code = array("test" , "cat" , "cat", "test" , "this", "that", "then");
$counts = array_count_values($code);
$max = max($counts);
$top = array_keys($counts, $max);
var_dump($max, $top);
/*
int(2)
array(2) {
[0]=>
string(4) "test"
[1]=>
string(3) "cat"
}
*/