<p>change <code>$products[] =</code> to <code>$products =</code> in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their titleVariation fields you should use <code>'elementType' =&gt; Entry::Class</code></p>

<pre><code>'pricelist/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
    return [
        'elementType' =&gt; Entry::Class,
        'criteria' =&gt; [
            'section' =&gt; 'products',
            'descendantOf' =&gt; $entryId,
        ],
        'transformer' =&gt; function(Entry $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry-&gt;orderItem as $product ) {
                $products = [
                    'title' =&gt; $product-&gt;titleVariation,
                ];
            }
            return $products;
        },
    ];
},
</code></pre>

CC BY-SA 4.0
2019-06-27T13:42:01.177Z
Thanks for the respond @HendrikEng. It will always overwrite the key so last item in array will return. It means that if entry field has more entries only last one will be returned.
Dominik Krulak
CC BY-SA 4.0
2019-06-27T13:51:46.320Z
do you want to get all title Variation fields of each entry in the section products?
HendrikEng
CC BY-SA 4.0
2019-06-27T13:59:38.183Z
updated the answer, it will now get all entries in products and loop over all orderItems and the titleVariation fields
HendrikEng
CC BY-SA 4.0
2019-06-27T14:00:37.257Z
First I access descendants of `entryId`. Than in descendant entry I access entry field `orderItem`. Than I must loop for in fields that I want to access. Field `titleVariation` is just an example so the code is not complicated.
Dominik Krulak
CC BY-SA 4.0
2019-06-27T14:08:57.010Z
Well, it still returns last entry from entry field `orderItem`. It seems like `$products[] = [ ... ];` is necessary to insert `$product` to array.
Dominik Krulak
CC BY-SA 4.0
2019-06-27T14:18:04.983Z
now i finally got what you mean , yes you will need it since the data array holds each entry which then holds an array of the titleVariation objects.
HendrikEng
CC BY-SA 4.0
2019-06-27T14:21:02.680Z
So it seems it's the only way to do it. So it means more work in JavaScript :( Thank you for the help @HendrikEng
Dominik Krulak
CC BY-SA 4.0
2019-06-27T13:33:59.073Z
2019-06-27T14:00:52.827Z
2019-06-27T14:00:52.827Z
HendrikEng
<p>I'm having difficulties to remove an additional set of square brackets or avoid including them within array.</p>

<p>My JavaScript code currently accesses array's object at 0 index and therefore I'd have to loop in one level deeper array. Which I'd like to avoid if possible.</p>

<p><strong>This is my endpoint:</strong></p>

<pre><code>'pricelist/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
    return [
        'elementType' =&gt; ElementType::Entry,
        'criteria' =&gt; [
            'section' =&gt; "products",
            'descendantOf' =&gt; $entryId,
        ],
        'transformer' =&gt; function(EntryModel $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry-&gt;orderItem as $product ) {
                $products[] = [
                    'title' =&gt; $product-&gt;titleVariation,
                ];
            }
            return $products;
        },
    ];
},
</code></pre>

<p><strong>Returned JSON array is:</strong></p>

<pre><code>{"data":
    [
        [
            {
                "title": "a",
            },
            {
                "title": "b",
            },
            {
                "title": "c",
            }
        ]
    ]
}
</code></pre>

<p><strong>Expected JSON array output:</strong></p>

<pre><code>{"data":
    [
        {
            "title": "a",
        },
        {
            "title": "b",
        },
        {
            "title": "c",
        }
    ]
}
</code></pre>

<p>I have almost the same scenario from <a href="https://craftcms.stackexchange.com/questions/21572/element-api-remove-additional-set-of-square-brackets-from-json-string/21574#21574"><strong>this question</strong></a> but the answer wasn't my issue.</p>

CC BY-SA 4.0
2019-06-27T09:33:26.667Z
Did you checked `'title' => $product->titleVariation` while adding to array.I mean is there any need to wrap `title in  [ ]`
inrsaurabh
CC BY-SA 4.0
2019-06-27T10:26:22.370Z
@inrsaurabh not wrapping it gives a syntax error because the key must be within `[]`. And furthermore not assigning key name gives the same output - two dimensional array.
Dominik Krulak
<plugin-elementapi><craft2>
Return JSON array without extra square brackets
# Return JSON array without extra square brackets

(Asked by: username_0 on 2019-06-27)

I'm having difficulties to remove an additional set of square brackets or avoid including them within array.
My JavaScript code currently accesses array's object at 0 index and therefore I'd have to loop in one level deeper array. Which I'd like to avoid if possible.
This is my endpoint:
'pricelist/.json' => function($entryId) {
    return [
        'elementType' => ElementType::Entry,
        'criteria' => [
            'section' => "products",
            'descendantOf' => $entryId,
        ],
        'transformer' => function(EntryModel $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry->orderItem as $product ) {
                $products[] = [
                    'title' => $product->titleVariation,
                ];
            }
            return $products;
        },
    ];
},

Returned JSON array is:
{"data":
    [
        [
            {
                "title": "a",
            },
            {
                "title": "b",
            },
            {
                "title": "c",
            }
        ]
    ]
}

Expected JSON array output:
{"data":
    [
        {
            "title": "a",
        },
        {
            "title": "b",
        },
        {
            "title": "c",
        }
    ]
}

I have almost the same scenario from this question but the answer wasn't my issue.

**Tags:** plugin-elementapi, craft2

> **Comments:**
> **username_1**: Did you checked `'title' => $product->titleVariation` while adding to array.I mean is there any need to wrap `title in  [ ]`  
> **username_0**: @username_1 not wrapping it gives a syntax error because the key must be within `[]`. And furthermore not assigning key name gives the same output - two dimensional array.  

---

## 1 answers

### Answer by username_2 on 2019-06-27. Score: 1

change $products[] = to $products = in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their titleVariation fields you should use 'elementType' => Entry::Class
'pricelist/.json' => function($entryId) {
    return [
        'elementType' => Entry::Class,
        'criteria' => [
            'section' => 'products',
            'descendantOf' => $entryId,
        ],
        'transformer' => function(Entry $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry->orderItem as $product ) {
                $products = [
                    'title' => $product->titleVariation,
                ];
            }
            return $products;
        },
    ];
},

> **Comments:**
> **username_0**: Thanks for the respond @username_2. It will always overwrite the key so last item in array will return. It means that if entry field has more entries only last one will be returned.  
> **username_2**: do you want to get all title Variation fields of each entry in the section products?  
> **username_2**: updated the answer, it will now get all entries in products and loop over all orderItems and the titleVariation fields  
> **username_0**: First I access descendants of `entryId`. Than in descendant entry I access entry field `orderItem`. Than I must loop for in fields that I want to access. Field `titleVariation` is just an example so the code is not complicated.  
> **username_0**: Well, it still returns last entry from entry field `orderItem`. It seems like `$products[] = [ ... ];` is necessary to insert `$product` to array.  
> **username_2**: now i finally got what you mean , yes you will need it since the data array holds each entry which then holds an array of the titleVariation objects.  
> **username_0**: So it seems it's the only way to do it. So it means more work in JavaScript :( Thank you for the help @username_2  

---

