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().

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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样