<p>The answer on <a href="https://craftcms.stackexchange.com/a/1333/6817">the previous question</a> is a little bit off - you <strong>do not</strong> want to use <code>json_encode</code> to save the field. Just use the actual array:</p>

<pre><code>$tableValue = [
  [
    "col1": "Product #1",
    "col2": "5",
    "col3": "10",
    "col4": "15"
  ],
  [
    "col1": "Product #2",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  ],
  [
    "col1": "Product #3",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  ]
];

$entry-&gt;setContentFromPost(array(
  'tableField'=&gt; $tableValue,
));
</code></pre>

<p>Note that 'col1', 'col2', etc, refer to locations in the table. The handle of the table column is not used.</p>

CC BY-SA 4.0
2018-03-07T13:22:00.950Z
2018-10-03T06:48:12.093Z
2018-10-03T06:48:12.093Z
Dan Ott
<p>I'm dealing with the import plugin, processing a .csv file. 
in the .csv there's a column with <code>|</code> separated values that shoud be inserted in a table field.
The entry are created correctly, also the table field ( just 1 column ) has the correct number of rows, but the content is empty ( <code>$featureContent</code> length is > 0 ).
Here's the code, what am I missing?</p>

<pre><code>foreach ($fields as $handle =&gt; $value) {

   $value = explode('|', $value);
   $content[$handle] = array();

   foreach ( $value as $featureContent ) {                       
      $featuresRow = array( 'singleFeature' =&gt; $featureContent );
      array_push( $content[$handle], $featuresRow);                             
   }

 }
</code></pre>

<p>--- Edit --- </p>

<p>According to <a href="https://craftcms.stackexchange.com/a/1333/6817">this response</a> the table data is stored in Json format, so now I'm trying to create the table providing a json formatted string.
As this:</p>

<pre><code>[
  {"col1":"very beautiful"},
  {"col1":"really beautiful"},
  {"col1":"astonishing beautiful"}
</code></pre>

<p>]</p>

<p>Still, this doesn't work for me. It doesn't even creates the 3 rows as the other approach. </p>

<import><table-field><csv>
Saving to table field via plugin
# Saving to table field via plugin

(Asked by: username_0 on 2017-10-09)

I'm dealing with the import plugin, processing a .csv file. 
in the .csv there's a column with | separated values that shoud be inserted in a table field.
The entry are created correctly, also the table field ( just 1 column ) has the correct number of rows, but the content is empty ( $featureContent length is > 0 ).
Here's the code, what am I missing?
foreach ($fields as $handle => $value) {

   $value = explode('|', $value);
   $content[$handle] = array();

   foreach ( $value as $featureContent ) {                       
      $featuresRow = array( 'singleFeature' => $featureContent );
      array_push( $content[$handle], $featuresRow);                             
   }

 }

--- Edit --- 
According to this response the table data is stored in Json format, so now I'm trying to create the table providing a json formatted string.
As this:
[
  {"col1":"very beautiful"},
  {"col1":"really beautiful"},
  {"col1":"astonishing beautiful"}

]
Still, this doesn't work for me. It doesn't even creates the 3 rows as the other approach.

**Tags:** import, table-field, csv


---

## 1 answers

### Answer by username_1 on 2018-03-07. Score: 3

The answer on the previous question is a little bit off - you do not want to use json_encode to save the field. Just use the actual array:
$tableValue = [
  [
    "col1": "Product #1",
    "col2": "5",
    "col3": "10",
    "col4": "15"
  ],
  [
    "col1": "Product #2",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  ],
  [
    "col1": "Product #3",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  ]
];

$entry->setContentFromPost(array(
  'tableField'=> $tableValue,
));

Note that 'col1', 'col2', etc, refer to locations in the table. The handle of the table column is not used.


---

