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);
?>