dongxie45083 2014-12-17 20:54
浏览 34
已采纳

在PHP中使用fputcsv水平移动指针

I am trying to write 2 arrays to csv file using fputcsv.

But the problem is when writing the second array it goes to the end of the file and starts from there.

So basically I have these 2 sections to write in a csv file and it's writing in the following way

ID      Name1

1       AAA
2       BBB

Name2

CCC
DDD

What I want

ID      Name1      Name2

1       AAA        CCC
2       BBB        DDD

My code is

fputcsv($fh, array("ID","Name1"));
while($row = mysqli_fetch_array($weekly1, MYSQLI_NUM))
{
    fputcsv($fh, array($row[0],$row[1]));
}

fputcsv($fh, array("Name2"));
while($row = mysqli_fetch_array($weekly2, MYSQLI_NUM))
{
    fputcsv($fh, array($row[1]));
}

Is there any way to overcome this problem. Any help or suggestion is highly welcomed.Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dtlzdofl66441 2014-12-17 20:58
    关注

    First you should store ID and NAME1 in an array and only after getting NAME2 save to csv:

    fputcsv($fh, array("ID","Name1","Name2"));
    $rows = array();
    while($row = mysqli_fetch_array($weekly1, MYSQLI_NUM))
    {
        $rows[] = array($row[0],$row[1]));
    }
    
    $i = 0;
    while($row = mysqli_fetch_array($weekly2, MYSQLI_NUM))
    {
        fputcsv($fh, array_merge($rows[$i], array($row[1])));
        $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致