如果找不到数组项,则跳过插入?

时间:2016-01-06 12:21:04

标签: php arrays eloquent laravel-5.1

我目前正在将数据从json api url插入到我的数据库中,除了一些数组未完成填写,导致未定义的索引'错误。

有没有好办法解决这个问题?当然使用if isset语句检查每个项目索引,并且仅在可用时设置它将起作用,但对于每个条目来说这将是相当繁琐的。

        foreach($items['response']['groups'] as $item) {
            foreach($item['items'] as $item) {
                Bar::firstOrCreate([
                    'lat' => $item['venue']['location']['lat'],
                    'long' => $item['venue']['location']['lng'],
                    'postalCode' => $item['venue']['location']['postalCode'],
                    'city' => $item['venue']['location']['city'],
                    'state' => $item['venue']['location']['state'],
                    'country' => $item['venue']['location']['country'],
                    'address' => $item['venue']['location']['address'],
                    'rating' => $item['venue']['rating'],
                    'website' => $item['venue']['url'],
                ]);
            }
        }

5 个答案:

答案 0 :(得分:1)

使用最简单的方式:

foreach($items['response']['groups'] as $item1) {
            foreach($item1['items'] as $item) {
                Bar::firstOrCreate([
                    'lat' => isset($item['venue']['location']['lat'])?$item['venue']['location']['lat'] : '',
                    'long' => isset($item['venue']['location']['lng'])?$item['venue']['location']['lng'] : '',
                    'postalCode' => isset($item['venue']['location']['postalCode'])?$item['venue']['location']['postalCode'] : '',
                    'city' => isset($item['venue']['location']['city'])?$item['venue']['location']['city'] : '',
                    'state' => isset($item['venue']['location']['state'])?$item['venue']['location']['state'] : '',
                    'country' => isset($item['venue']['location']['country'])?item['venue']['location']['country'] : '',
                    'address' => isset($item['venue']['location']['address'])?$item['venue']['location']['address'] : '',
                    'rating' => isset($item['venue']['rating'])?$item['venue']['rating'] : '',
                    'website' => isset($item['venue']['url'])?$item['venue']['url'] : '',
                ]);
            }
        }

答案 1 :(得分:0)

您的问题是,您在每次迭代时重写 $item的值。

foreach($items['response']['groups'] as $item) {
    foreach($item['items'] as $item) {
        // after first iteration here `$item` doesn't have `items` key
    }

在每个foreach中使用不同的变量。

答案 2 :(得分:0)

你必须检查某个地方是否存在索引!如果您不想在控制器中执行此操作,则可以使用一个实体来检查每个字段:

<?php
class MyEntity 
{

    private $lat;
    private $long;
    private $postalcode;
    ...

    public static function createInstanceFromArray(array $item)
    {
        if (isset($item['lat']) {
            $this->setLat($item['lat']);
        }
        ... and so on

        // you could also do it automatic :
        foreach($item as $key => $value) {
            $methodName = 'set' . ucfirst($key);
            if (method_exists($this, $methodName)) {
                $this->{$methodName}($value);
            }
        }
    }

    public function setLat($lat) 
    {
        $this->lat = $lat;

        return $this;
    }

    public function getLat()
    {
        return $this->lat;
    }
}

然后您可以对原始代码执行以下操作:

foreach($items['response']['groups'] as $item) {
    foreach($item['items'] as $subItem) {
        $myEntity = MyNamespace\MyEntity::cretaInstanceFromArray($subItem['venue']);
    }
}

然后,您可以使用您的方法设置实体的字段:

'lat' => $myEntity->getLat();
....

这样,即使数组字段不存在,实体也会返回默认值。

如果您不想使用对象,可以使用带有默认值的array_merge来确保您没有undefindex索引:

$item = array_merge([
    'lat' => '',
    'long' => '',
    'postalCode' => '',
    ...
], $item['venue']);

并将该处理放在一个函数中作为例子。

答案 3 :(得分:0)

检查每个数组是否完整,只检查foreach($items['response']['groups'] as $item) { foreach($item['items'] as $item) { if( isset($item['venue']) && isset($item['venue']['location']) && isset($item['venue']['location']['lat']) && isset($item['venue']['location']['lng']) && isset($item['venue']['location']['postalCode']) && isset($item['venue']['location']['city']) && isset($item['venue']['location']['state']) && isset($item['venue']['location']['country']) && isset($item['venue']['location']['address']) && isset($item['venue']['location']['lng']) && isset($item['venue']['rating']) && isset($item['venue']['url']) ) Bar::firstOrCreate([ 'lat' => $item['venue']['location']['lat'], 'long' => $item['venue']['location']['lng'], 'postalCode' => $item['venue']['location']['postalCode'], 'city' => $item['venue']['location']['city'], 'state' => $item['venue']['location']['state'], 'country' => $item['venue']['location']['country'], 'address' => $item['venue']['location']['address'], 'rating' => $item['venue']['rating'], 'website' => $item['venue']['url'], ]); } } 是否完整:

Expandablelistview

答案 4 :(得分:0)

您可以使用这些简单的步骤

  1. 从不包含值的嵌套数组中删除所有键,然后
  2. 删除所有空的嵌套数组。

    $ yourArray = array_map('array_filter',$ yourArray);

    $ yourArray = array_filter($ yourArray);