<p>Seems a bit unnecessary to cast it to an object.  This should work identically:</p>

<pre><code>'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
    ];
},
</code></pre>

CC BY-SA 3.0
2016-08-29T21:25:33.570Z
2016-08-29T21:25:33.570Z
Brad Bell
<p>I have an object <code>look</code> which contains an object categories from type of <code>array</code>. Currently I have to do the following to display the categories proper in the response.</p>

<pre><code>'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
                ];
            },
        ]
    ]
</code></pre>

<p>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.</p>

<p>A structure could looks something like this</p>

<pre><code>[
    {
      name: "ads"
      categories: [
         {
            name: "asdasda"
            subCategories: [
                name: "asdasdsa"
           ]
          }
      ]
    }
]
</code></pre>

<plugins><plugin-elementapi>
Nested objects containing array in the Elements API plugin
# Nested objects containing array in the Elements API plugin

(Asked by: username_0 on 2016-08-29)

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' => [
        'looks.json' => [
            'elementType' => 'Entry',
            'criteria' => ['section' => 'looks'],
            'transformer' => function(EntryModel $entry) {

                // Create an array of all the photo URLs
                $categories = [];
                foreach ($entry->categories as $category) {
                    $object = (object) ['id' => $category->id, 'title' => $category->title];
                    $categories[] = $object;
                }
                return [
                    'title' => $entry->title,
                    'id' => $entry->id,
                    'categories' => $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"
           ]
          }
      ]
    }
]

**Tags:** plugins, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2016-08-29. Score: 2

Seems a bit unnecessary to cast it to an object.  This should work identically:
'transformer' => function(EntryModel $entry) {

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

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

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


---

