doufei4418 2014-09-16 20:15
浏览 124

根据导入的CSV文件中的列将行分组在一起

I have a CSV file which has many rows in, like the following:

Client Product Quantity Unit Amount Total 1 Prod1 1 10 10 1 Prod2 2 15 30 2 Prod1 2 12 24 2 Prod2 1 5 5

etc...

i want to be able to import the CSV file using PHP and group each client together and display the total cost for each client.

I have my import function etc...

if(is_uploaded_file($_FILES['file']['tmp_name'])) {
    echo "<h3>" . "File ". $_FILES['file']['name'] ." uploaded successfully." . "</h3><br><br>";
}

$handle = fopen($_FILES['file']['tmp_name'], "r");
fgetcsv($handle);

//then loop through each row
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

}

but im not sure what to do inside the while loop

if possible, i want to do this without using a database

  • 写回答

2条回答 默认 最新

  • duanqian9503 2014-09-16 20:39
    关注
    $clients = array();
    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
         if(!array_key_exists($data[0],$clients))
             $clients[$data[0]] = array();
          array_push($clients[$data[0]],$data);
    }
    
    
    //then loop over each client in your array
    foreach($clients as $clientId => $clientEntries)
    {
    // combine or print records...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么