2017-07-27 99 views
0

如何從對象中獲取數組?我試圖去空的數組,以便我可以驗證它是空的狀態。從對象獲取數組| | php

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

print_r($object); 

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

} 

輸出

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

) 
+1

其中兩個(OP)由我所描述的情況很有意思你可以對它進行類型轉換,比如(array)$ array – Nick

+0

@melkawakibi在這裏看看https://stackoverflow.com/a/4345609/2520628 –

+0

我改變了代碼,現在它是一個雄辯的對象,其中有一個數組裏面 – melkawakibi

回答

2

目的是類型鋒\收集,這意味着它具有訪問一組的方法。

Eloquent\Collection | available methods

類別提供一組方法來驗證所述對象內部的陣列的狀態。

  • 的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); 

} 
0

試試這樣:

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

它給出這個錯誤,該集合實例上不存在Property [array]。 – melkawakibi