Lucas Ln
Lucas Ln

Reputation: 77

Group Array from .csv | PHP

I have this code and what i'm trying to do is , group the data coming from the .csv by Player , and then group by year and league , so for example , i will have faker -> 2021 -> lck->data ; ->2020->lck->data

and sometimes when a player has played more than one league in a year , faker->2021->lck->data | 2021->kespa->data

the problem is when i show the kills(image) , the year 2020 is adding the kills from 2021 plus kills from 2020. and what i want is 2020 show the kills from that league and that year and the same with 2021.

The result im Getting :

Faker => 2021 => kills [1,2,3] ; 2020 => kills [1,2,3,6,9,12];

The expected result is :
Faker => 2021 => kills [1,2,3] ; 2020 => kills [6,9,12]

how can i achieve that ?

Thats the csv

gameid,datacompleteness,url,league,year,split,playoffs,date,game,patch,playerid,side,position,player,team,champion .....

thats my code;

<?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>';
}

?>

enter image description here

Upvotes: 1

Views: 228

Answers (1)

Syscall
Syscall

Reputation: 19780

You are adding to $kills array, without taking care of the year. So, when you parse the year 2021, the $kills array already contains the 2020 data.

You could create an empty array the first time (each new year), and then, fill it.

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;
    }
}

See a working demo.

Upvotes: 1

Related Questions