Php 使用Guzzle Laravel插入数据时未定义的偏移量

Php 使用Guzzle Laravel插入数据时未定义的偏移量,php,laravel,guzzle,Php,Laravel,Guzzle,我使用Guzzle将数据从第三方api插入数据库。一切似乎都很好,直到api中不存在其中一个字段。它带来了一个错误:未定义的偏移量,所以我估计这就是问题所在。 这是我的指挥时间表: $client = new Client(['headers' => ['Accept' => 'application/json']]); $res = $client->request('GET', 'https://example.com/api/apiKey

我使用Guzzle将数据从第三方api插入数据库。一切似乎都很好,直到api中不存在其中一个字段。它带来了一个错误:未定义的偏移量,所以我估计这就是问题所在。 这是我的指挥时间表:

        $client = new Client(['headers' => ['Accept' => 'application/json']]); 
        $res = $client->request('GET', 'https://example.com/api/apiKey=XXXXXXXXXXXXXXXXXXX');
        $data = json_decode($res->getBody()->getContents(),true);
        $events = $data['Data'];

        foreach($events as $item)
        {
                        DB::table('apidata')->updateOrInsert([
                'matchID'=>$item['matchID']],
                [
                'matchID'=>$item['matchID'] ?? null,
                'startTime'=>date('Y-m-d H:i', strtotime($item['startTime'])) ?? null,
                'timeLive'=>$item['timeLive'] ?? null,
                'homeTeam'=>$item['homeTeamInfo']['homeTeam'] ?? null,
                'homeGoals' =>$item['homeTeamInfo']['homeGoals'] ?? null,
                'awayGoals'=>$item['awayTeamInfo']['awayGoals'] ?? null,
                'awayTeam'=>$item['awayTeamInfo']['awayTeam'] ?? null,
那么,在api带有不存在字段的情况下,避免未定义偏移量错误的最佳方法是什么呢?。提前感谢。

您使用startTime元素的方式不适合未找到的值。这个空位仅在您尝试将其转换为时间和日期后使用

'startTime'=>date('Y-m-d H:i', strtotime($item['startTime'])) ?? null,
在这种情况下,您最好使用isset的旧方法

和…一样

bcdiv($item['odds'][0]['regular'][0]['odds'][0]['decimalValue'],1,2) ?? null


对于要插入的每个字段,添加一个检查是否已设置

DB::table('apidata')->updateOrInsert([
            'matchID'=>$item['matchID']],
            [
            'matchID'=>isset($item['matchID']) ? $item['matchID'] : '',
            'startTime'=>isset($item['startTime']) ? date('Y-m-d H:i', strtotime($item['startTime'])) : '',
            'timeLive'=>isset($item['timeLive']) ? $item['timeLive'] : '',
            'homeTeam'=>isset($item['homeTeamInfo']['homeTeam']) ? $item['homeTeamInfo']['homeTeam'] : '',
            'homeGoals' =>isset($item['homeTeamInfo']['homeGoals']) ? $item['homeTeamInfo']['homeGoals'] : '',
            'awayGoals'=>isset($item['awayTeamInfo']['awayGoals']) ? $item['awayTeamInfo']['awayGoals'] : '',
            'awayTeam'=>isset($item['awayTeamInfo']['awayTeam']) ? $item['awayTeamInfo']['awayTeam'] : '',

向我们显示updateOrInsert方法体以及传递的arguments@RonnyKid我需要更多的信息。你能贴出错误的痕迹吗?谢谢你的回答。我无法在表达式的结果上使用isset,您可以使用null!==当我放置isset方法时使用表达式。如果您查看答案中的代码,它在isset$item['startTime']中使用。我明白了。但是这个字段bcdiv$item['bits'][0]['regular'][0]['bits'][0]['decimalValue']出现了问题,1,2??无效的设置它的最佳方法是什么?同样地,设置$item['bits'][0]['regular'][0]['bits'][0]['decimalValue']使用bcdiv$a,1,2??null的问题是bcdiv将尝试使用该值进行计算并导致错误,如果您想将其设置为有效值,也可以使用bcdiv$a??1,1,2。但由于您似乎希望空值为null,我不确定您是否可以在不使用isset的情况下执行此操作。谢谢您的回答。我想这种方法也适用于空字段,对吗?是的..你可以用!测试空值时为空。它将工作。
isset($item['odds'][0]['regular'][0]['odds'][0]['decimalValue']) ?
       bcdiv($item['odds'][0]['regular'][0]['odds'][0]['decimalValue'],1,2) 
      : null
DB::table('apidata')->updateOrInsert([
            'matchID'=>$item['matchID']],
            [
            'matchID'=>isset($item['matchID']) ? $item['matchID'] : '',
            'startTime'=>isset($item['startTime']) ? date('Y-m-d H:i', strtotime($item['startTime'])) : '',
            'timeLive'=>isset($item['timeLive']) ? $item['timeLive'] : '',
            'homeTeam'=>isset($item['homeTeamInfo']['homeTeam']) ? $item['homeTeamInfo']['homeTeam'] : '',
            'homeGoals' =>isset($item['homeTeamInfo']['homeGoals']) ? $item['homeTeamInfo']['homeGoals'] : '',
            'awayGoals'=>isset($item['awayTeamInfo']['awayGoals']) ? $item['awayTeamInfo']['awayGoals'] : '',
            'awayTeam'=>isset($item['awayTeamInfo']['awayTeam']) ? $item['awayTeamInfo']['awayTeam'] : '',