<p>I figured this out. The table field is expecting JSON:</p>

<pre class="lang-php prettyprint-override"><code>$entry = new Entry();
$entry-&gt;sectionId = 1;
$entry-&gt;typeId = 1;
$entry-&gt;title = $title;
$entry-&gt;setFieldValues([
  "field_handle" =&gt; '[{"col1":"hello","col2":"world"},{"col1":"more","col2":"stuff"}]'
]);
</code></pre>

CC BY-SA 4.0
2021-11-07T04:28:27.267Z
Great answer thank you! There is the `json_encode([ 'col1' => 'hello', 'col2' => 'world' ])` which may be useful for others :)
Clark Nelson
CC BY-SA 4.0
2019-09-06T21:06:29.223Z
2019-09-06T21:06:29.223Z
David Jones
<p>I'm trying to insert an entry along with values for a table field. According to the <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#method-setfieldvalues" rel="nofollow noreferrer">docs</a>, the <code>setFieldValues</code> method takes an array of values in the format <code>handle =&gt; value</code>, but it doesn't specify how the value should be formatted for each field type.</p>

<p>Here's my best guess at how it might work for the table field:</p>

<pre class="lang-php prettyprint-override"><code>$entry = new Entry();
$entry-&gt;sectionId = 1;
$entry-&gt;typeId = 1;
$entry-&gt;title = $title;
$entry-&gt;setFieldValues([
  "field_handle" =&gt; [
    ["column_1_handle" =&gt; "hello", "column_2_handle" =&gt; "world"], // Row 1
    ["column_1_handle" =&gt; "more", "column_2_handle" =&gt; "stuff"] // Row 2
  ]
]);
</code></pre>

<p>This inserts the correct number of rows into the table field, but the values are empty, so I'm guessing I'm structuring the array incorrectly.</p>

<p>Any ideas?</p>

<modules><table-field>
How to set field values for the table field type in Craft 3
# How to set field values for the table field type in Craft 3

(Asked by: username_0 on 2019-09-06)

I'm trying to insert an entry along with values for a table field. According to the docs, the setFieldValues method takes an array of values in the format handle => value, but it doesn't specify how the value should be formatted for each field type.
Here's my best guess at how it might work for the table field:
$entry = new Entry();
$entry->sectionId = 1;
$entry->typeId = 1;
$entry->title = $title;
$entry->setFieldValues([
  "field_handle" => [
    ["column_1_handle" => "hello", "column_2_handle" => "world"], // Row 1
    ["column_1_handle" => "more", "column_2_handle" => "stuff"] // Row 2
  ]
]);

This inserts the correct number of rows into the table field, but the values are empty, so I'm guessing I'm structuring the array incorrectly.
Any ideas?

**Tags:** modules, table-field

---

## 1 answers

### Answer by username_0 on 2019-09-06. Score: 5

I figured this out. The table field is expecting JSON:
$entry = new Entry();
$entry->sectionId = 1;
$entry->typeId = 1;
$entry->title = $title;
$entry->setFieldValues([
  "field_handle" => '[{"col1":"hello","col2":"world"},{"col1":"more","col2":"stuff"}]'
]);

> **Comments:**
> **username_1**: Great answer thank you! There is the `json_encode([ 'col1' => 'hello', 'col2' => 'world' ])` which may be useful for others :)  

---

