2010-06-25 39 views
1

stdClass::__set_state(array(
    'zone1' => 
    array (
    0 => 
    stdClass::__set_state(array(
     'id' => '123', 
     'owner' => '234', 
     ... 
    )), 
 

嗨,添加在數據謝靈運一個stdClass的對象(PHP)

我基本都有點拍,所以我有這個麻煩...我需要創建上述結構,但我不知道如何...

+1

'stdClass'沒有'__set_state()'方法。 – 2010-06-25 09:19:09

+1

@Daniel Egeberg:你說得對,它沒有那種方法。它只是如何導出對象(這對stdClass沒什麼意義)var_export((object)array('zone1'=> array((object)array('id'=>'123','owner'=>' 234' ))));' – salathe 2010-06-25 09:44:26

回答

7
$a = new stdclass; 
$a->zone1 = array(); 
$a->zone1[0] = new stdclass; 
$a->zone1[0]->id = "123"; 
$a->zone1[0]->owner = "234"; 

另外,依賴於一個事實,即數組轉換爲stdClass的對象時鑄成對象:

$a = (object) array(
    "zone1" => array(
     (object) array("id" => "123", "owner" => "234"), 
    ), 
); 

爲此,var_export給出:

 
stdClass::__set_state(array(
    'zone1' => 
    array (
    0 => 
    stdClass::__set_state(array(
     'id' => '123', 
     'owner' => '234', 
    )), 
), 
)) 

需要注意的是,有丹尼爾指出,stdClass實際上並沒有一個__set_state方法。我想你只是通過給出var_export的輸出來舉例說明變量的結構。序列化應該用serialize代替。