提问人:lky 提问时间:12/2/2021 更新时间:12/2/2021 访问量:189
为什么在检查未定义索引是否为空时会抛出错误?
Why am I throw an error for undefined index when I'm checking whether it's empty?
问:
我收到以下错误:
错误异常:/Users/User/Sites/Site/app/Jobs/MigrateData.php:67 中未定义的数组键 0
这在我的工作的一个实例中是正确的,$this->schools 是一个空数组,因此不应该点击创建。对不起,我有点不确定为什么会抛出错误。
$this->data = [];
$i=0;
foreach($core_data as $core) {
$dataCode = DataCode::where('code', $core->code)->first();
if ($dataCode instanceof DataCode) {
$this->data[$i]['data_id'] = $dataCode->id;
$this->data[$i]['data_name'] = $dataCode->name;
}
$i++;
}
if (!empty($this->data)) {
$data = Data::create([
'first_name' => $this->data[0]['data_name']
]);
}
对我哪里出错有什么帮助吗?
答:
0赞
RiggsFolly
12/2/2021
#1
像这样加载数组,它将使用 添加一个新事件,然后将数组添加到该新事件中[]
$this->data[] = ['data_id' => $dataCode->id,
'data_name' => $dataCode->name];
评论
$i
$dataCode instanceof DataCode
$i++
if
$i
first_name
break
if