<p>The table field contains an array of all columns, I don't really see where you problem is since you could just do <code>data[0].flavorText[0].text</code> in your template and as you already explained you can see the direct output so you can just address the index in the array.</p>

<pre><code>$block-&gt;flavorText[0]-&gt;text 
</code></pre>

<p>will return you the text within the first column</p>

<p>In order to get the values from all columns you'll have to loop through them</p>

<pre><code>$values = [];
foreach($block-&gt;flavorText as $column){
    $values[] = $column['text'];
}
</code></pre>

CC BY-SA 3.0
2018-03-15T15:07:05.687Z
I need to return all of them minus the other junk fields.
Dustin C
CC BY-SA 3.0
2018-03-15T15:12:03.717Z
Then you should loop through them. This has actually nothing to do with Craft. It's just basic PHP knowledge
Robin Schambach
CC BY-SA 3.0
2018-03-15T15:17:58.237Z
where in the code should I loop through them? Wherever I try it, I get errors. Assume my knowledge of PHP is limited.
Dustin C
CC BY-SA 3.0
2018-03-15T15:25:42.990Z
you could just `var_dump` your field in order to see what it contains, but I edited the code. You have a 2D array with all columns and rows. You loop the columns and get the correct value from your row
Robin Schambach
CC BY-SA 3.0
2018-03-15T15:56:23.097Z
Tried putting the for loop between case 'theData' and $bodyBlocks, and it gave me an error about an incorrect foreach statement. Where does this physically need to sit in the order of things?
Dustin C
CC BY-SA 3.0
2018-03-15T22:08:51.240Z
That is the correct place. Please make sure to check if your field value is not null. Maybe that causes the issue. The code works for me. You use a normal table field and not a field from a plugin?
Robin Schambach
CC BY-SA 3.0
2018-03-15T14:54:23.003Z
2018-03-15T15:25:06.553Z
2018-03-15T15:25:06.553Z
Robin Schambach
<p>I'm using element API to pull JSON, and I have it working pretty well with a matrix field, but I need to pull a single column out of a table field inside of the matrix, and I don't even know where to start.</p>

<p>We're using this as a headless way to pull data into a game, and I need to pull the flavor text, and just have it output one field in the table, not everything.</p>

<pre><code>'api/locations/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
        return [
            'elementType' =&gt; Entry::class,
            'criteria' =&gt; ['section' =&gt; 'locations', 'slug' =&gt; $slug],
            'pretty' =&gt; true,
            'one' =&gt; true,
            'transformer' =&gt; function (craft\elements\Entry $entry) {
                $bodyBlocks = [];
                foreach ($entry-&gt;dataMatrix as $block) {
                    switch ($block-&gt;type-&gt;handle) {
                        case 'theData':
                            $bodyBlocks[] = [
                                'zone' =&gt; $block-&gt;zone,
                                'zoneName' =&gt; $block-&gt;zoneName,
                                'alternateZone' =&gt; $block-&gt;alternateZone,
                                'zoneBanner' =&gt; $block-&gt;zoneBanner,
                                'backgroundAsset' =&gt; $block-&gt;backgroundAsset,
                                'propAsset' =&gt; $block-&gt;propAsset,
                                'flavorText' =&gt; $block-&gt;flavorText,
                            ];
                            break;
                    }
                }
                return [
                    'data' =&gt; $bodyBlocks,
                ];
            }
        ];
    },
</code></pre>

<p>This is currently giving me this output for the table field:</p>

<pre><code>"flavorText": [
            {
                "col1": "1",
                "col2": "Arbitrary flavor text here",
                "col3": "",
                "title": "1",
                "text": "Arbitrary flavor text here",
                "icon": ""
            }
        ]
</code></pre>

<p>All I want is the stuff in the 'text' field, nothing else. There is no documentation anywhere on how to deal with Table Fields in the Element API, much less how to do it within a matrix. I'm also not sure how to loop through fields inside of a matrix that require loops to pull the data normally.</p>

<p>Anyone have any idea how I can get this output?</p>

<plugin-elementapi>
Element API -- getting table data from a matrix
# Element API -- getting table data from a matrix

(Asked by: username_0 on 2018-03-15)

I'm using element API to pull JSON, and I have it working pretty well with a matrix field, but I need to pull a single column out of a table field inside of the matrix, and I don't even know where to start.
We're using this as a headless way to pull data into a game, and I need to pull the flavor text, and just have it output one field in the table, not everything.
'api/locations/.json' => function($slug) {
        return [
            'elementType' => Entry::class,
            'criteria' => ['section' => 'locations', 'slug' => $slug],
            'pretty' => true,
            'one' => true,
            'transformer' => function (craft\elements\Entry $entry) {
                $bodyBlocks = [];
                foreach ($entry->dataMatrix as $block) {
                    switch ($block->type->handle) {
                        case 'theData':
                            $bodyBlocks[] = [
                                'zone' => $block->zone,
                                'zoneName' => $block->zoneName,
                                'alternateZone' => $block->alternateZone,
                                'zoneBanner' => $block->zoneBanner,
                                'backgroundAsset' => $block->backgroundAsset,
                                'propAsset' => $block->propAsset,
                                'flavorText' => $block->flavorText,
                            ];
                            break;
                    }
                }
                return [
                    'data' => $bodyBlocks,
                ];
            }
        ];
    },

This is currently giving me this output for the table field:
"flavorText": [
            {
                "col1": "1",
                "col2": "Arbitrary flavor text here",
                "col3": "",
                "title": "1",
                "text": "Arbitrary flavor text here",
                "icon": ""
            }
        ]

All I want is the stuff in the 'text' field, nothing else. There is no documentation anywhere on how to deal with Table Fields in the Element API, much less how to do it within a matrix. I'm also not sure how to loop through fields inside of a matrix that require loops to pull the data normally.
Anyone have any idea how I can get this output?

**Tags:** plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2018-03-15. Score: 1

The table field contains an array of all columns, I don't really see where you problem is since you could just do data[0].flavorText[0].text in your template and as you already explained you can see the direct output so you can just address the index in the array.
$block->flavorText[0]->text 

will return you the text within the first column
In order to get the values from all columns you'll have to loop through them
$values = [];
foreach($block->flavorText as $column){
    $values[] = $column['text'];
}

> **Comments:**
> **username_0**: I need to return all of them minus the other junk fields.  
> **username_1**: Then you should loop through them. This has actually nothing to do with Craft. It's just basic PHP knowledge  
> **username_0**: where in the code should I loop through them? Wherever I try it, I get errors. Assume my knowledge of PHP is limited.  
> **username_1**: you could just `var_dump` your field in order to see what it contains, but I edited the code. You have a 2D array with all columns and rows. You loop the columns and get the correct value from your row  
> **username_0**: Tried putting the for loop between case 'theData' and $bodyBlocks, and it gave me an error about an incorrect foreach statement. Where does this physically need to sit in the order of things?  
> **username_1**: That is the correct place. Please make sure to check if your field value is not null. Maybe that causes the issue. The code works for me. You use a normal table field and not a field from a plugin?  

---

