Nested objects containing array in the Elements API plugin
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
    ];
},