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

two array element save in same tabel through php

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

//$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 循环 ??

请帮我解决这个问题。

您需要使用array_merge() not array_combine()

array_merge 实际上收集了两个数组 array_combine 通过使用一个数组作为键和另一个因为它的价值

我认为你需要使用 array_merge() instead of 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!";
}

希望对您有所帮助!