Home php Parsing an array of objects in php

Parsing an array of objects in php

Author

Date

Category

Good afternoon.
Faced a problem. There is an array of objects of the form:

Array
(
  [0] = & gt; Tbl2 Object
    (
      [tableFields: protected] = & gt; Array
        (
          [id] = & gt; 1
          [some_field] = & gt; test1
          [tables] = & gt; Tbl Object
            (
              [tableFields: protected] = & gt; Array
                (
                  [id] = & gt; 4
                  [name] = & gt; TEST INSERT 2
                  [text] = & gt; fetch by id = 4
                  [fk_tbl2] = & gt; 1
                )
            )
        )
    )
[1] = & gt; Tbl2 Object
  (
    [tableFields: protected] = & gt; Array
      (
        [id] = & gt; 2
        [some_field] = & gt; test2
        [tables] = & gt; Tbl Object
          (
            [tableFields: protected] = & gt; Array
              (
                [id] = & gt; 2
                [name] = & gt; TEST8
                [text] = & gt; fetch by id = 2
                [fk_tbl2] = & gt; 2
              )
          )
      )
  )
[2] = & gt; Tbl2 Object
  (
    [tableFields: protected] = & gt; Array
      (
        [id] = & gt; 2
        [some_field] = & gt; test2
        [tables] = & gt; Tbl Object
          (
            [tableFields: protected] = & gt; Array
              (
                [id] = & gt; 3
                [name] = & gt; TEST INSERT
                [text] = & gt;
                [fk_tbl2] = & gt; 2
              )
          )
      )
  )
 )

In the example, there are three Tbl2 objects, two of them are repeated (with id = 2) due to the fact that they contain different Tbl subobjects. I just can’t bring the array to the following form:

Array
(
  [0] = & gt; Tbl2 Object
    (
      [tableFields: protected] = & gt; Array
        (
          [id] = & gt; 1
          [some_field] = & gt; test1
          [tables] = & gt; Tbl Object
            (
              [tableFields: protected] = & gt; Array
                (
                  [id] = & gt; 4
                  [name] = & gt; TEST INSERT 2
                  [text] = & gt; fetch by id = 4
                  [fk_tbl2] = & gt; 1
                )
            )
        )
    )
[1] = & gt; Tbl2 Object
  (
    [tableFields: protected] = & gt; Array
      (
        [id] = & gt; 2
        [some_field] = & gt; test2
        [tables] = & gt; Array (
          [0] = & gt; Tbl Object
          (
            [tableFields: protected] = & gt; Array
              (
                [id] = & gt; 2
                [name] = & gt; TEST8
                [text] = & gt; fetch by id = 2
                [fk_tbl2] = & gt; 2
              )
          ),
          [1] = & gt; Tbl Object
          (
            [tableFields: protected] = & gt; Array
              (
                [id] = & gt; 3
                [name] = & gt; TEST INSERT
                [text] = & gt;
                [fk_tbl2] = & gt; 2
              )
          )
      )
  )
 )

That is, get TWO Tbl2 objects, but the one with id = 2 must contain an array of Tbl objects.


Answer 1, authority 100%

Try this

$ obj_merged = (object) array_merge ((array) $ OBJ1, (Array) $ OBJ2);

or so

$ OBJ_MERGED = (OBJECT) Array_merge_Recursive ((Array) $ OBJ1, (Array) $ OBJ2);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions