我有包含团队名称的 CSV 列表(这是一行):
Teams
Red
Green | Red | Yellow
Red
Yellow | Green | Yellow
Red
Green
Yellow | Yellow | Red
Red
Green
Red
Yellow
我需要能够计算每种颜色并计算前 4 种颜色和它们出现的次数。
我怎样才能做到这一点?例如,如果我尝试使用:
$teams = file('count.csv');
$count[] = (array_count_values($colors));
print_r($count);
我得到:
Array ( [0] => Array ( [Teams ] => 1
[Red ] => 5 [Green | Red | Yellow ] => 1 [Yellow | Green | Yellow ] => 1 [Green ] => 2 [Yellow | Yellow | Red ] => 1 [Yellow] => 1 ) )
这不是很有用。并且我怎么能将这些山谷相互比较以获得前 4 名?
任何已知的技巧如何做到这一点?先感谢您!
好的,再试一次:
$inputfile = 'count.csv';
$inputHandle = fopen($inputfile, "r");
while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE) {
$teams = $data[0];
$teams = explode('|', $teams);
}
$count[] = (array_count_values($teams));
print("<pre>".print_r($count, true)."</pre>");
我明白了
Array
(
[0] => Array
(
[Yellow] => 1
)
)
我究竟做错了什么?