2017-08-29 226 views
1

將一個對象推入另一個對象時出現錯誤。但第二個對象是一個數組,並且在數組內部有一個對象。我怎樣才能解決這個問題,因爲我想補充一點,到我的對象將數組對象推入另一個數組對象中php

我的目標就是這樣的

我想添加的對象2Object1

Objet1

stdClass Object 
(
    [id_laporan_pemeriksa] => 5 
    [no_pkpt] => SNE 
    [tgl_pkpt] => 2010 
    [no_penugasan] => ST-4000/PW25/2/2017 
    [tgl_penugasan] => 2017-08-09 
    [judul_laporan] => Masukkan Kode disini 
    [no_laporan] => LBINA-9000/PW25/2/2017 
    [tgl_laporan] => 2017-08-01 
    [tahun_anggaran_penugasan] => 2009 
    [nilai_anggaran_penugasan] => 10000000 
    [realisasi_anggaran_penugasan] => 100000000 
    [jenis_anggaran_penugasan] => Utang 
    [sumber_laporan] => Inspektorat Maluku 
    [nama_sumber_penugasan] => PKPT 
    [nama_ketua_tim] => Abdul Rofiek, Ak. 
    [nama_pengendali_teknis] => Alfian Massagony, S.E. 
    [nama_unit_penugasan] => Irban Wil. I 
    [nama_penugasan] => Penjaminan 
    [nama_sub_penugasan] => Audit 
    [id_s_sub_penugasan] => 010105 
    [nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian. 
) 

Object2

stdClass Object 
(
    [id] => 3 
    [data_sebab] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 4 
        [data_rekomendasi] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 4 

            [data_tindak_lanjut] => Array 
             (
              [0] => stdClass Object 
               (
                [id] => 9 

                [tgl_tindak_lanjut] => 0000-00-00 
               ) 

             ) 

           ) 

          [1] => stdClass Object 
           (
            [id] => 5 
            [id_rekomendasi] => 
            [data_tindak_lanjut] => Array 
             (
              [0] => stdClass Object 
               (
                [id] => 10 
                [id_tindak_lanjut] => 
                [tgl_tindak_lanjut] => 0000-00-00 
               ) 

              [1] => stdClass Object 
               (
                [id] => 11 
                [id_tindak_lanjut] => 
                [tgl_tindak_lanjut] => 0000-00-00 
               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

) 

我已經試過

$Object1['data']->$Object2; 

但我得到了一個錯誤

不能使用類型爲stdClass的對象作爲數組

回答

3

添加$Object2作爲語法$Object1的財產是:

$Object1->Object2 = $Object2; 

或者:

$Object1->{'Object2'} = $Object2; 
+0

好的,這個語法可以工作。但是當我將它推入Object1時,我得到了一個數組。我希望它是stdClassObject 而我如何將整個object2變成一個數組? – Gagantous

+0

你可以使用'(array)$ object'。 – Marty

+0

我必須爲每個['data_sebab'],[data_rekomendasi]和[data_tindak_lanjut]數組添加(object)成爲object?或者有一個爲什麼要一次改變它們? ?我已經添加了這個$​​ Object1-> Object2 =(object)$ Object2;但它只有Object2數組發生了變化,而不是內部數組@Marty – Gagantous

3

它應該是:

$Object1->data = $Object2; // it will create data element with obj2 as value 
1

由於對象是對象而不是數組,使用:

$Object1['data']->$Object2; 

不會工作。但是,執行以下操作將起作用:

$Object1->data = $Object2;