dtbi27903 2016-06-10 06:18
浏览 329
已采纳

使用Laravel将JSON数据插入文件

I am inserting JSON data to a file with Laravel, I have 300 000 records and I want to insert them by block of 5000 records.

 public function fileput($start,$end,$n) {
        $final = [];
        $res = DB::table('company')
                ->Where('company_id', '>',$start)
                ->Where('company_id', '<',$end)
                ->get();

        foreach ($res as $k => $v) {
            $id = $v->company_id;
            $index = 10000000 + $id;
            unset($res[$k]->company_id);
            unset($res[$k]->company_data);
            $arr = array('index' => ['_id' => $index]);
            $ind = json_encode($arr);
            $data = json_encode($v);
            array_push($final, $ind, $data);
        }
        $written = File::put('file.txt', $final);
} 

Problem is, after entering first 5000, when incrementing to 10000, first 5000 records got erased.

Therefore how can I insert all the records in one file?

  • 写回答

1条回答 默认 最新

  • dsvq5069 2016-06-10 06:33
    关注

    Try using File::append() instead of File::put().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题