两个数组元素通过php保存在同一个tabel中

时间:2018-02-20 05:33:19

标签: php laravel laravel-5.2

我在两个变量中有两个数组值。我想把两个都保存在表格中 同时为此我使用了这段代码:

//$ec_country=$request->input('exc');
//$ey=$request->input('exy');

//In current case its comming Null

foreach (array_combine($c,$y) as $e1=>$y1) 
{
    DB::table('table')->insert([
        ['id' => $id, 'c_id' => $y1,'yr'=>$y1,'added_date'=>$date]
    ]);
}

我的HTML代码是:

<div class="form-group row no-padding ">
@foreach($result['c'] as $ed)
<div class="col-md-6 col-xs-12 ">
    <label class="fvb">ec</label>
    <select name="EC[]" class="form-control " id="ec">
        @foreach($result['te'] as $tec)
        <option value="{{tce->id}}" > {{$tce->ce}}</option>
        @endforeach
    </select>
</div>
<div class="col-md-6 col-xs-12">
    <label class="english">Years</label>
    <select name="ey[]" id="er" class="form-control">
        @for($i=1;$i<=10;$i++)
        <option value="{{$i}}"  >{{ $i}}</option>
        @endfor
    </select>
</div>
@endforeach
</div>

当我在两个数组中都有值但是数组是时,它的工作正常 空白它返回我的错误如:

array_combine() expects parameter 1 to be array, null given

所以我的问题是,有没有其他方法可以解决这个问题 而不是array_combine()使用foreach循环??

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

我认为您需要使用array_merge()而不是array_combine()

试试这样:

if(!empty($ec && $ey)) {
  foreach (array_merge($ec,$ey) as $e=>$y) 
  {
    DB::table('table')->insert([
        ['wid' => $id, 'cid' => $expcont,'ey'=>$y,'added_date'=>$date]
    ]);
  }
} else {
  return "Any return statement!";
}

希望这能帮到你!

答案 1 :(得分:0)

您需要使用array_merge()而不是array_combine()

array_merge 实际上收集了两个数组, array_combine 通过一个数组用于键而另一个数组用于其值来创建一个数组