1

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object

My object just like this

I want to add the the Object2 into Object1

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
                                                )

                                        )

                                )

                        )

                )

        )

)

I have tried

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

But i got an error

Cannot use object of type stdClass as array

3 Answers 3

5

The syntax of adding $Object2 as a property of $Object1 is:

$Object1->Object2 = $Object2;

Or:

$Object1->{'Object2'} = $Object2;
Sign up to request clarification or add additional context in comments.

9 Comments

Okay, this syntax works. but i got an array instead when i am push it into Object1. i want it to be stdClassObject And how do i change the entire object2 into an array ?
You can use (array)$object.
i must add (object) for each ['data_sebab'],[data_rekomendasi],and [data_tindak_lanjut] array to become object ? or there is a why to change them all at once ? ? i have added this $Object1->Object2 = (object) $Object2; but its only the Object2 array that changed, not the inside array @Marty
You can hackishly do something like $Object1 = json_decode(json_encode($Object1)).
$Object->{'0'}->id
|
4

It should be:

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

Comments

1

As the objects are objects and not arrays, using:

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

wont work. However doing the following will work:

$Object1->data = $Object2;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.