我有这段代码,我想要做的是,将来自.csv的数据按球员分组,然后按年份和联赛分组,例如,我将有faker -> 2021 -> lck->data;->2020->lck->data。
有时,当一个球员在一年中打了一个以上的联赛时,伪造者->2021->lck->data_ 2021->kespa->data ->data->
问题是当我显示死亡(图片),2020年是从2021年加上2020年的杀戮。我想要的是,2020年显示出联盟和那一年的死亡人数,2021年的情况也是一样。
其结果是:
Faker => 2021 =>杀1,2,3,3,2020年=>杀1,2,3,6,9,12;
预期结果是:
Faker => 2021 =>杀死1,2,3人;2020年=>杀死6,9,12人
我怎样才能做到这一点?
那是csv
游戏,数据,网址,联盟,年份,分裂,季后赛,日期,比赛,补丁,球员,位置,球员,球队,冠军……
这是我的准则;
<?php
$csvFileData = file('./datalck.csv');
$dataMatch = [];
foreach ($csvFileData as $lines) {
$data[] = str_getcsv($lines);
}
foreach ($dataMatch as $matchs) {
// So here i'm grouping the array by player
//[3] is the position for the league
//[4] is the position for year
//[13] is the position of player name ,
//[23] The position of kills
if ($matchs[13] != NULL and $matchs[13] != "") {
$group[$matchs[13]][] = [
'Player' => $matchs[13],
"kills" => $matchs[23],
'league' => $matchs[3],
'year' => $matchs[4],
];
}
}
foreach ($group as $players => $p) {
$kills = [];
foreach ($p as $op) {
$kills[] = $op['kills'];
$group2[$op['Player']][$op['year']][$op['league']] = [
"Player" => $op['Player'],
"kills" => $kills,
"league" => $op['league'],
"year" => $op['year'],
];
}
}
foreach ($group2 as $op2) {
echo '<pre>';
var_dump(($group2));
echo '</pre>';
}
?>

发布于 2021-02-19 07:04:06
您将添加到$kills数组中,而不考虑年份。因此,在解析年份2021时,$kills数组已经包含了2020数据。
您可以在第一次(每个新年)创建一个空数组,然后填充它。
foreach ($group as $players => $p)
{
foreach ($p as $op)
{
// variables for readability
$plyr = $op['Player'];
$year = $op['year'];
$leag = $op['league'];
$kills = $op['kills'];
// create the initial state
if (!isset($group2[$plyr][$year][$leag]))
{
$group2[$plyr][$year][$leag] = [
"Player" => $plyr,
"league" => $leag,
"year" => $year,
"kills" => [], // empty array
];
}
// add value to array for the player/year/league :
$group2[$plyr][$year][$leag]['kills'][] = $kills;
}
}看一个工作演示。
https://stackoverflow.com/questions/66268173
复制相似问题