所以我有一个数组,有点像:
$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”
谢谢你:)答案 0 :(得分:0)
echo key(array_count_values($concat));
答案 1 :(得分: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);
答案 2 :(得分: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);