Answers.Body: <p>You should be able to foreach the $block->product to include the selected values (or labels)</p>
  
  <pre><code>return [
      'endpoints' =&gt; [
          'fullmembers.json' =&gt; [
              'elementType' =&gt; 'User',
              'criteria' =&gt; ['group' =&gt; 'fullMembership'],
              'transformer' =&gt; function(UserModel $member) {
                  $businessCategories = [];
                  foreach ($member-&gt;businessTypeMarketSectorsProducts as $block) {
                      switch ($block-&gt;type-&gt;handle) {
                          case 'businessCategory':
                              $productValues = [];
                              foreach ($block-&gt;product as $product) {
                                  if ($product-&gt;selected) {
                                      $productValues[] = $product-&gt;value;
                                  }
                              }
                              $businessCategories[] = [
                                  'businessType' =&gt; $block-&gt;businessType-&gt;value,
                                  'marketSector' =&gt; $block-&gt;marketSector-&gt;value,
                                  'product' =&gt; $productValues
                              ];
                              break;
                      }
                  }
  
                  return [
                      'id' =&gt; $member-&gt;id,
                      'companyName' =&gt; $member-&gt;membersCompanyName,
                      'addressLine3' =&gt; $member-&gt;membersAddressLine3,
                      'lat' =&gt; $member-&gt;memberAddress-&gt;lat,
                      'lng' =&gt; $member-&gt;memberAddress-&gt;lng,
                      'email' =&gt; $member-&gt;email,
                      // 'profile' =&gt; '/directory/company/' . $member-&gt;id . '/' . $member-&gt;username,
                      'profile' =&gt; UrlHelper::getUrl("directory/company/{$member-&gt;id}"),
                      'businessCategories' =&gt; $businessCategories
                  ];
              },
          ]
      ]
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-29T09:41:16.217Z
Answers.Text: Thanks chris. You've nailed it! The output is now: {"businessType":"Manufacture","marketSector":"Contract","product":["Beds","Mattresses","Seating \/ upholstery"]}]},
Answers.UserDisplayName: Adam Menczykowski
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-29T09:40:08.180Z
Answers.LastActivityDate: 2016-01-29T09:40:08.180Z
Answers.UserDisplayName: chris
Body: <p>Using the Element API, I want to return the values from checkboxes (the $block->product field). These checkboxes are within a matrix that I am already gathering data from. Currently am doing this:</p>
  
  <p>
  
  <pre><code>return [
  'endpoints' =&gt; [
      'fullmembers.json' =&gt; [
          'elementType' =&gt; 'User',
          'criteria' =&gt; ['group' =&gt; 'fullMembership'],
          'transformer' =&gt; function(UserModel $member) {
              $businessCategories = [];
              foreach ($member-&gt;businessTypeMarketSectorsProducts as $block) {
                  switch ($block-&gt;type-&gt;handle) {
                      case 'businessCategory':
  
                          $businessCategories[] = [
                              'businessType' =&gt; $block-&gt;businessType-&gt;value,
                              'marketSector' =&gt; $block-&gt;marketSector-&gt;value,
                              'product' =&gt; $block-&gt;product //Here you can see I'm calling the product field... 
                          ];
                          break;
                  }
              }
  
              return [
                  'id' =&gt; $member-&gt;id,
                  'companyName' =&gt; $member-&gt;membersCompanyName,
                  'addressLine3' =&gt; $member-&gt;membersAddressLine3,
                  'lat' =&gt; $member-&gt;memberAddress-&gt;lat,
                  'lng' =&gt; $member-&gt;memberAddress-&gt;lng,
                  'email' =&gt; $member-&gt;email,
                  // 'profile' =&gt; '/directory/company/' . $member-&gt;id . '/' . $member-&gt;username,
                  'profile' =&gt; UrlHelper::getUrl("directory/company/{$member-&gt;id}"),
                  'businessCategories' =&gt; $businessCategories
              ];
          },
      ]
  ]
  ];
  </code></pre>
  
  <p>Here is a sample response:</p>
  
  <pre><code>    {
    "id": "1506",
    "companyName": "Airsprung Group Plc",
    "addressLine3": "",
    "lat": "51.33188454",
    "lng": "-2.20657303",
    "email": "email@email.co.uk",
    "profile": "http:\/\/craft.fira\/directory\/company\/1506",
    "businessCategories": [{
      "businessType": "Manufacture",
      "marketSector": "Domestic",
      "product": {
        "0": {
          "label": "Beds",
          "value": "Beds",
          "selected": true
        },
        "1": {
          "label": "Mattresses",
          "value": "Mattresses",
          "selected": true
        },
        "2": {
          "label": "Seating \/ upholstery",
          "value": "Seating \/ upholstery",
          "selected": true
        }
      }
    }, {
      "businessType": "Manufacture",
      "marketSector": "Contract",
      "product": {
        "0": {
          "label": "Beds",
          "value": "Beds",
          "selected": true
        },
        "1": {
          "label": "Mattresses",
          "value": "Mattresses",
          "selected": true
        },
        "2": {
          "label": "Seating \/ upholstery",
          "value": "Seating \/ upholstery",
          "selected": true
        }
      }
    }]
  }
  </code></pre>
  
  <p>I would like the response to just show the values of the product checkboxes. Can you suggest a way to do this so that I am not returning the label and selected states? Ideally something like this:</p>
  
  <pre><code>"product": {"Beds", "Mattresses", "Seating \/ upholstery"}
  </code></pre>
  
Tags: <plugin-elementapi>
Title: Element API + checkboxes
ThreadText: # Element API + checkboxes
  
  (Asked by: username_0 on 2016-01-29)
  
  Using the Element API, I want to return the values from checkboxes (the $block->product field). These checkboxes are within a matrix that I am already gathering data from. Currently am doing this:
  
  ```
  return [
  'endpoints' => [
      'fullmembers.json' => [
          'elementType' => 'User',
          'criteria' => ['group' => 'fullMembership'],
          'transformer' => function(UserModel $member) {
              $businessCategories = [];
              foreach ($member->businessTypeMarketSectorsProducts as $block) {
                  switch ($block->type->handle) {
                      case 'businessCategory':
  
                          $businessCategories[] = [
                              'businessType' => $block->businessType->value,
                              'marketSector' => $block->marketSector->value,
                              'product' => $block->product //Here you can see I'm calling the product field... 
                          ];
                          break;
                  }
              }
  
              return [
                  'id' => $member->id,
                  'companyName' => $member->membersCompanyName,
                  'addressLine3' => $member->membersAddressLine3,
                  'lat' => $member->memberAddress->lat,
                  'lng' => $member->memberAddress->lng,
                  'email' => $member->email,
                  // 'profile' => '/directory/company/' . $member->id . '/' . $member->username,
                  'profile' => UrlHelper::getUrl("directory/company/{$member->id}"),
                  'businessCategories' => $businessCategories
              ];
          },
      ]
  ]
  ];
  
  ```
  
  Here is a sample response:
  
  ```
      {
    "id": "1506",
    "companyName": "Airsprung Group Plc",
    "addressLine3": "",
    "lat": "51.33188454",
    "lng": "-2.20657303",
    "email": "email@email.co.uk",
    "profile": "http:\/\/craft.fira\/directory\/company\/1506",
    "businessCategories": [{
      "businessType": "Manufacture",
      "marketSector": "Domestic",
      "product": {
        "0": {
          "label": "Beds",
          "value": "Beds",
          "selected": true
        },
        "1": {
          "label": "Mattresses",
          "value": "Mattresses",
          "selected": true
        },
        "2": {
          "label": "Seating \/ upholstery",
          "value": "Seating \/ upholstery",
          "selected": true
        }
      }
    }, {
      "businessType": "Manufacture",
      "marketSector": "Contract",
      "product": {
        "0": {
          "label": "Beds",
          "value": "Beds",
          "selected": true
        },
        "1": {
          "label": "Mattresses",
          "value": "Mattresses",
          "selected": true
        },
        "2": {
          "label": "Seating \/ upholstery",
          "value": "Seating \/ upholstery",
          "selected": true
        }
      }
    }]
  }
  
  ```
  
  I would like the response to just show the values of the product checkboxes. Can you suggest a way to do this so that I am not returning the label and selected states? Ideally something like this:
  
  ```
  "product": {"Beds", "Mattresses", "Seating \/ upholstery"}
  
  ```
  
  **Tags:** plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-01-29. Score: 1 | Accepted answer
  
  You should be able to foreach the $block->product to include the selected values (or labels)
  
  ```
  return [
      'endpoints' => [
          'fullmembers.json' => [
              'elementType' => 'User',
              'criteria' => ['group' => 'fullMembership'],
              'transformer' => function(UserModel $member) {
                  $businessCategories = [];
                  foreach ($member->businessTypeMarketSectorsProducts as $block) {
                      switch ($block->type->handle) {
                          case 'businessCategory':
                              $productValues = [];
                              foreach ($block->product as $product) {
                                  if ($product->selected) {
                                      $productValues[] = $product->value;
                                  }
                              }
                              $businessCategories[] = [
                                  'businessType' => $block->businessType->value,
                                  'marketSector' => $block->marketSector->value,
                                  'product' => $productValues
                              ];
                              break;
                      }
                  }
  
                  return [
                      'id' => $member->id,
                      'companyName' => $member->membersCompanyName,
                      'addressLine3' => $member->membersAddressLine3,
                      'lat' => $member->memberAddress->lat,
                      'lng' => $member->memberAddress->lng,
                      'email' => $member->email,
                      // 'profile' => '/directory/company/' . $member->id . '/' . $member->username,
                      'profile' => UrlHelper::getUrl("directory/company/{$member->id}"),
                      'businessCategories' => $businessCategories
                  ];
              },
          ]
      ]
  ];
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_1. You've nailed it! The output is now: {"businessType":"Manufacture","marketSector":"Contract","product":["Beds","Mattresses","Seating \/ upholstery"]}]},  
  
  ---
  
  