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/<slug:{slug}>.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,
                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?

1 Answer 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.


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'];
  • I need to return all of them minus the other junk fields.
    – Dustin C
    Commented Mar 15, 2018 at 15:07
  • Then you should loop through them. This has actually nothing to do with Craft. It's just basic PHP knowledge Commented Mar 15, 2018 at 15:12
  • 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
    Commented Mar 15, 2018 at 15:17
  • 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 Commented Mar 15, 2018 at 15:25
  • 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
    Commented Mar 15, 2018 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.