【发布时间】:2021-02-18 21:07:09
【问题描述】:
我有这段代码,我想做的是,将来自 .csv 的数据按 Player 分组,然后按年份和联赛分组,例如,我将有 faker -> 2021 -> lck ->数据; ->2020->lck->数据
有时当一名球员一年打过多个联赛时,faker->2021->lck->data | 2021->kespa->数据
问题是,当我显示击杀数(图片)时,2020 年将添加 2021 年的击杀数和 2020 年的击杀数。 我想要的是 2020 年显示该联盟和那一年的击杀数,与 2021 年相同。
我得到的结果:
Faker => 2021 => 杀死 [1,2,3] ; 2020 => 杀死 [1,2,3,6,9,12];
预期结果是:
Faker => 2021 => 杀死 [1,2,3] ; 2020 => 杀死 [6,9,12]
我怎样才能做到这一点?
这就是 csv
gameid,datacompleteness,url,league,year,split,季后赛,日期,游戏,补丁,playerid,side,position,player,team,champion .....
这是我的代码;
<?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>';
}
?>
【问题讨论】:
-
改进您的问题并添加格式化的 CSV 数据示例
-
正如@Bernhard 所说,请在您的问题中包含更多信息,请使用代码标签而不是图像,以便其他人可以更轻松地复制您的编码器。
-
我添加了一些 cmets 和部分 csv ,@Bernhard
标签: php arrays multidimensional-array