Jump to content

Build complex Data structure with array and stdClass


patrickhertling

Recommended Posts

Hi.
I would like to build a complex data structure in a foreach bucle and return it after

foreach ($array as $row) {
  // Here we get groupname and item_info for each row.
}

I want an array of groups(stdClass).

Each group has info, and a list of items(stdClass).

array(
  stdClass Group_1 (
    info
    list = array (
      stdClass item_1 (
        info
      )
      stdClass item_2 (..)
      ...
    )
  )
  ...
  stdClass Group_n (...)
)

Any help to build this would be apreciated. Thank you.

Hehe, sorry about that. Please let me explain again:

 

We have an array of stdClass elements: 

array(element_1, element_2, ..., element_n)

Each element is like:

element_k : {order: int, name: "name", group_name: "name", group_order: int}

Ok. Now I want to build an array / stdClass based data structure like followes: 

array:[group_1 : {name : "name",
                  order : int,
                  items : [ item_1 : {name : "name", order : int },
                            ...
                            item_n : {...}
                          ]
                  },
       ...
       group_n : {...}
      ]

I was thinking doing it in a loop, but maybe there is a better way.

Thank you very much for your time.

Regards

Patrick

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.