Nested objects containing array in the Elements API plugin I have an object look which contains an object categories from type of array. Currently I have to do the following to display the categories proper in the response.

'endpoints' =&gt; [
        'looks.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; ['section' =&gt; 'looks'],
            'transformer' =&gt; function(EntryModel $entry) {

                // Create an array of all the photo URLs
                $categories = [];
                foreach ($entry-&gt;categories as $category) {
                    $object = (object) ['id' =&gt; $category-&gt;id, 'title' =&gt; $category-&gt;title];
                    $categories[] = $object;
                }
                return [
                    'title' =&gt; $entry-&gt;title,
                    'id' =&gt; $entry-&gt;id,
                    'categories' =&gt; $categories
                ];
            },
        ]
    ]


Is there a more elegant way to doing this? Especially because I'll have a lot of more cases where I have to deal with arrays in objects and arrays in objects and so on.

A structure could looks something like this

[
    {
      name: "ads"
      categories: [
         {
            name: "asdasda"
            subCategories: [
                name: "asdasdsa"
           ]
          }
      ]
    }
]
Seems a bit unnecessary to cast it to an object.  This should work identically:

'transformer' =&gt; function(EntryModel $entry) {

    // Create an array of all the photo URLs
    $categories = [];

    foreach ($entry-&gt;categories as $category) {
        $categories[] = ['id' =&gt; $category-&gt;id, 'title' =&gt; $category-&gt;title];
    }

    return [
        'title' =&gt; $entry-&gt;title,
        'id' =&gt; $entry-&gt;id,
        'categories' =&gt; $categories
    ];
},