dqczgtem06898 2015-10-14 08:57
浏览 24
已采纳

如何在db laravel中保存多个数组

im getting these data from service

value = 1:1,2:0,3:1,4:0,5:1,6:1,7:0,8:0,9:1,10:0

and i splitt it using arrayas

$result = array();
                foreach (explode(',',$value) as $sub)
                {
                    $subAry = explode(':',$sub);
                    $result[$subAry[0]] = $subAry[1];

                }
                return $result;

here how its return
  "1": "1",
  "2": "0",
  "3": "1",
  "4": "0",
  "5": "1",
  "6": "1",
  "7": "0",
  "8": "0",
  "9": "1",
  "10": "0"
}

i want to save these values in table in two columns, please advice

first number set should goes to $value column and other one should goes to $type column,

here is my controller,

$DriverPreDepartureItems = new DriverPreDepartureItems();
                    $DriverPreDepartureItems->user_id = $driver_id->id;

                    $DriverPreDepartureItems->shift_id = $find_shift->id;
                    $DriverPreDepartureItems->type = ;
                    $DriverPreDepartureItems->value = ;

                    $DriverPreDepartureItems->save();

pls advice

  • 写回答

1条回答 默认 最新

  • dongtong7990 2015-10-14 09:35
    关注

    Since you have that kind of results, you can still access them using foreach loop and save them to your table just like below

    foreach ($result as $key => $value) {
        /* do your saving codes here
        * the values of $key are (1,2,3,4,..,10)
        * and $values will be (1 or 0) */
    
        $DriverPreDepartureItems = new DriverPreDepartureItems();
        $DriverPreDepartureItems->user_id = $driver_id->id;
    
        $DriverPreDepartureItems->shift_id = $find_shift->id;
        $DriverPreDepartureItems->type = $value;
        $DriverPreDepartureItems->value = $key;
    
        $DriverPreDepartureItems->save();
    
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化