Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T13:42:01.177Z
Answers.Text: 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.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T13:51:46.320Z
Answers.Text: do you want to get all title Variation fields of each entry in the section products?
Answers.UserDisplayName: HendrikEng
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T13:59:38.183Z
Answers.Text: updated the answer, it will now get all entries in products and loop over all orderItems and the titleVariation fields
Answers.UserDisplayName: HendrikEng
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T14:00:37.257Z
Answers.Text: 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.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T14:08:57.010Z
Answers.Text: Well, it still returns last entry from entry field `orderItem`. It seems like `$products[] = [ ... ];` is necessary to insert `$product` to array.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T14:18:04.983Z
Answers.Text: 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.
Answers.UserDisplayName: HendrikEng
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T14:21:02.680Z
Answers.Text: So it seems it's the only way to do it. So it means more work in JavaScript :( Thank you for the help @HendrikEng
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-27T13:33:59.073Z
Answers.LastActivityDate: 2019-06-27T14:00:52.827Z
Answers.LastEditDate: 2019-06-27T14:00:52.827Z
Answers.UserDisplayName: HendrikEng
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-27T09:33:26.667Z
Comments.Text: Did you checked `'title' => $product->titleVariation` while adding to array.I mean is there any need to wrap `title in  [ ]`
Comments.UserDisplayName: inrsaurabh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-27T10:26:22.370Z
Comments.Text: @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.
Comments.UserDisplayName: Dominik Krulak
Tags: <plugin-elementapi><craft2>
Title: Return JSON array without extra square brackets
ThreadText: # 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/<entryId:\d+>.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**](https://craftcms.stackexchange.com/questions/21572/element-api-remove-additional-set-of-square-brackets-from-json-string/21574#21574) 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/<entryId:\d+>.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  
  
  ---
  
  