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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试