Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中输出重复次数最多的值_Php_Arrays_String_Recurring Events - Fatal编程技术网

在PHP中输出重复次数最多的值

在PHP中输出重复次数最多的值,php,arrays,string,recurring-events,Php,Arrays,String,Recurring Events,我有一个数组,有点像: $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 谢谢:您可以使用array\u

我有一个数组,有点像:

$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


谢谢:

您可以使用array\u count\u值和array\u键输出结果:

echo key(array_count_values($concat));
$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);

您可以使用array_count_值来获取一个以实例为键、以频率为值的数组。然后需要将数组从高到低进行排序,维护索引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);