View Single Post
Old 02-14-2022, 12:13 PM  
ZTT
Confirmed User
 
ZTT's Avatar
 
Industry Role:
Join Date: Apr 2019
Posts: 657
I don't know what you're trying to accomplish overall, but to group and add the figures together you could...

Process the original CSV to look like this:

Book1,001,12,340
Book2,002,10,260
Book1,001,8,240
Book5,005,2,60

And from that you'd produce a CSV like this:

Book1,001,20,580
Book2,002,10,260
Book5,005,2,60

From this:

Code:
<?php

$data = array_map('str_getcsv', file('data.csv'));

foreach($data as $datas){
    $x = $datas[0];
    if(isset($book[$x])){
        $book[$x][2] = $book[$x][2] + $datas[2];
        $book[$x][3] = $book[$x][3] + $datas[3];
    }else{
        $book[$x] = $datas;
    }
}

$sum = fopen('sum.csv', 'w');

foreach ($book as $books) {
    fputcsv($sum, $books);
}

fclose($sum);
?>
__________________
__________________
ZTT is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote