我有一个数组说:
$array = array(1, 5, 2, 3, 3, 4, 5, 1)
我想转换此数组并将值作为键并将其频率作为值输出应该如下所示:
$array = array (
"1" => 2,
"2" => 1,
"3" => 2,
"4" => 1,
"5" => 2
)
我知道如何在 python 中做到这一点。但我必须用 PHP 来做。我已经在 python 中使用以下代码实现了这个:python 代码:
d = {}
for j in tweets: // tweet is a array of integers as given above $array
d[j] = d.get(j, 0) + 1 // This will make a dictionary with the values in the array as key and its frequency as value of the dictionary .. Get() dynamically increase the value
请帮忙。