从对象获取数组| PHP

时间:2017-07-27 09:14:47

标签: php arrays laravel eloquent

如何从对象中获取数组?我试图进入空数组,以便我可以验证它的空状态。

$object = Illuminate\Database\Eloquent\Collection Object;

print_r($object);

if(empty($object->array)){

}

输出

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
        )

)

2 个答案:

答案 0 :(得分:2)

该对象的类型为Eloquent \ Collection,这意味着它可以访问一组方法。

Eloquent\Collection | available methods

Collection提供了一组方法来验证Object内部数组的状态。其中两个在我(OP)所描述的案例中很有意思:

  • IsEmpty
  • IsNotEmpty

实施

$website = $this->websitedb->findOneByUrl($this->url);

if($website->isNotEmpty()){

    $uniqueId = rand() . $website[0]->id;

    //save scan to database
    $this->scan = $this->scandb->create($website[0]->id, $uniqueId);

    $this->scandb->createModule($this->scan->id, $options);

}

答案 1 :(得分:0)

试试这样:

if ($object->array === array()) {
    echo 'this is explicitly an empty array!';
}