0

所以我有一个数组,有点像:

$concat=array("DARK HORSE,KATY PERRY", "DARK HORSE,KATY PERRY", "WHEN IT RAINS,PARAMORE", "LITHIUM,NIRVANA")
//$concat = song and artist together separated by a comma

我需要输出出现次数最多的值,所以在上面的数组中我需要输出字符串 = "DARK HORSE,KATY PERRY"

谢谢你 :)

4

3 回答 3

0
echo key(array_count_values($concat));
于 2014-04-09T21:14:17.590 回答
0

您可以使用 array_count_values 和 array_keys 来输出结果:

$concat=array("DARK HORSE,KATY PERRY", "DARK HORSE,KATY PERRY", "WHEN IT RAINS,PARAMORE", "LITHIUM,NIRVANA");
//counts frequencies
$count_array = array_count_values($concat);

//gets the keys instead of the values
$count_keys = array_keys($count_array);

//echoes only the first key
echo current($count_keys);

//Or print all values and keys
print_r($count_array);
于 2014-04-09T21:14:28.967 回答
0

您可以使用 array_count_values 获取以实例为键、频率为值的数组。然后你需要将数组从高到低排序,保持索引(arsort)这很重要。

所以:

//your array
$concat=array("DARK HORSE,KATY PERRY", "DARK HORSE,KATY PERRY", "WHEN IT RAINS,PARAMORE", "LITHIUM,NIRVANA")

//get all the frequencies
$frequencies = array_count_values($concat);

//make sure to sort it since array_count_values doesn't return a sorted array
arsort($frequencies);

//reset the array because you can't trust keys to get the first element by itself
reset($frequencies);

//get the first key
echo key($frequencies);
于 2014-04-09T21:14:33.347 回答