<p>You can obviously perform your import directly from the CSV files (or remote database), reformat the data and then import; or import the data into temporary craft text fields and then in a separate operation, move the data in craft. Personally I would opt for the first solution to avoid the extra work of creating temp fields (and allow me to import again later if needed).</p>

<p>This example is for importing into a table field (importing into a matrix field would be quite different and slightly more complicated).</p>

<pre><code>// loop through each row in your CSV file or results on external database query
foreach($result as $row)
{
    $entry = new EntryModel();
    $entry-&gt;sectionId   = 1;
    $entry-&gt;typeId      = 1;
    $entry-&gt;authorId    = 1;
    $entry-&gt;enabled     = true;

    // create temp array to store feature data
    $myTableFieldData = [];
    array_push($myTableFieldData, array("col1" =&gt; $row['feature1']));
    array_push($myTableFieldData, array("col1" =&gt; $row['feature2']));
    array_push($myTableFieldData, array("col1" =&gt; $row['feature3']));
    // ...

    $entry-&gt;getContent()-&gt;setAttributes(array(
        'myTableFieldHandle' =&gt; $myTableFieldData,
    ));

    // if you want to check the data first before importing
    // uncomment this and comment out saveEntry below
    //var_dump($entry);

    $success = craft()-&gt;entries-&gt;saveEntry($entry);

    if (!$success)
    {
        $errors = $entry-&gt;getErrors();
        foreach ($errors as $error) {
            MyImportPlugin::log('Error:'.$error[0], LogLevel::Error);
        }
    }
    else
    {
        MyImportPlugin::log('Successfully saved entry "'.$entry-&gt;id.'"', LogLevel::Info);
    };

}
</code></pre>

CC BY-SA 3.0
2015-04-08T15:56:39.603Z
Do I just save this to a .php file and add to plugin folder, or do I just run in a PHP file on its own?  Never build a plugin before.  I appreciate the help Douglas.
Bryan Garrant
CC BY-SA 3.0
2015-04-08T16:14:46.817Z
That question is a bit more complicated. And I guess I assumed that you were already familiar with plugins. This example represents an (incomplete) method in a Service file which would be one part of your plugin. You would call this service method from either an `init` function in your main plugin file; via a Controller method (defined in your plugins controller file that itself would be triggered by hitting a specific url or clicking a button, etc); or from a Variable method (defined in your plugins variable's file and called from a template).
Douglas McDonald
CC BY-SA 3.0
2015-04-08T16:14:51.600Z
I would suggest you start by reading the [plugin documentation](https://buildwithcraft.com/docs/plugins/introduction) and/or installing a plugin template like [BusinessLogic](https://github.com/lindseydiloreto/craft-businesslogic) to get you started. How much data are we talking about? It might be easier to do it manually.
Douglas McDonald
CC BY-SA 3.0
2015-04-08T16:21:59.857Z
341 entries with about 10 feature fields each.
Bryan Garrant
CC BY-SA 3.0
2015-04-08T16:26:07.157Z
I could merge the 10 fields in the CSV using Excel, but how can I find the array format needed for a Matrix or Table import?
Bryan Garrant
CC BY-SA 3.0
2015-04-08T16:33:10.013Z
That's not a bad option. The best way to find the exact format you need would be to fill in a sample table field the way you want it and then look at the data in the craft database ('craft_content' table) using 'Sequel Pro' or 'phpMyAdmin'. If you want to take a stab at writing a plugin (which is not really that hard), I can probably walk you through as well. Although I might recommend an easier approach, knowing that you already have the data in craft.
Douglas McDonald
CC BY-SA 3.0
2015-04-08T16:41:12.240Z
But table field data in the craft database looks like this (repeated for as many rows as you need): `[{"col1":"myDataTextString","col2":"myDataTextString"},{"col1":"myDataTextString","col2":"myDataTextString"}]`
Douglas McDonald
CC BY-SA 3.0
2015-04-08T16:45:47.550Z
Might also want to do a manual test first before you spend the time reformatting your CSV file (i.e. format one entry in your CSV file manually in the proper craft table format and try and import it using the import plugin).
Douglas McDonald
CC BY-SA 3.0
2015-04-08T21:14:19.493Z
Trying the CSV merge method. Thanks Donald. I really appreciate it.
Bryan Garrant
CC BY-SA 3.0
2015-04-09T16:18:39.827Z
I still need some help with this import if anyone is interested.  My basic issue is this: 1. I have all data in a CSV with about 300 entries 2. I need to merge 10 plain text fields into a Craft Table field 3. I have 5 text fields containing image names that I want to import in Craft Asset field and link to entry ID 4. I have 1 text field containing PDF file name that I want to import into Craft Asset field and link to entry ID

Is anyone able to help with this import?
Bryan Garrant
CC BY-SA 3.0
2015-04-09T18:43:41.707Z
Bob Olde Hampsink helped me sort this one out. Thanks again Bob!
Bryan Garrant
CC BY-SA 3.0
2015-04-09T20:19:46.620Z
Thanks for your help Douglas.  I was able to get some of the data using your method as well.
Bryan Garrant
CC BY-SA 3.0
2015-04-10T02:08:11.437Z
Great! .........
Douglas McDonald
CC BY-SA 3.0
2015-04-08T15:54:34.177Z
2015-04-08T15:54:34.177Z
Douglas McDonald
<p>I have a site we are importing into Craft (using <a href="https://github.com/boboldehampsink/import/" rel="nofollow">https://github.com/boboldehampsink/import/</a>) and it has about 10 fields setup called feature1, feature2, ... feature10 that are Plain Text. After I import them, rather than leave them all as individual text fields, is there a way to merge them into a matrix block or table field?  I have the data in a CSV as well that I am using for the import.</p>

<matrix>
Is there a way to merge multiple Plain Text fields into a Matrix or Table field?
# Is there a way to merge multiple Plain Text fields into a Matrix or Table field?

(Asked by: username_0 on 2015-04-08)

I have a site we are importing into Craft (using <https://github.com/boboldehampsink/import/>) and it has about 10 fields setup called feature1, feature2, ... feature10 that are Plain Text. After I import them, rather than leave them all as individual text fields, is there a way to merge them into a matrix block or table field? I have the data in a CSV as well that I am using for the import.

**Tags:** matrix


---

## 1 answers

### Answer by username_1 on 2015-04-08. Score: 2

You can obviously perform your import directly from the CSV files (or remote database), reformat the data and then import; or import the data into temporary craft text fields and then in a separate operation, move the data in craft. Personally I would opt for the first solution to avoid the extra work of creating temp fields (and allow me to import again later if needed).
This example is for importing into a table field (importing into a matrix field would be quite different and slightly more complicated).
// loop through each row in your CSV file or results on external database query
foreach($result as $row)
{
    $entry = new EntryModel();
    $entry->sectionId   = 1;
    $entry->typeId      = 1;
    $entry->authorId    = 1;
    $entry->enabled     = true;

    // create temp array to store feature data
    $myTableFieldData = [];
    array_push($myTableFieldData, array("col1" => $row['feature1']));
    array_push($myTableFieldData, array("col1" => $row['feature2']));
    array_push($myTableFieldData, array("col1" => $row['feature3']));
    // ...

    $entry->getContent()->setAttributes(array(
        'myTableFieldHandle' => $myTableFieldData,
    ));

    // if you want to check the data first before importing
    // uncomment this and comment out saveEntry below
    //var_dump($entry);

    $success = craft()->entries->saveEntry($entry);

    if (!$success)
    {
        $errors = $entry->getErrors();
        foreach ($errors as $error) {
            MyImportPlugin::log('Error:'.$error[0], LogLevel::Error);
        }
    }
    else
    {
        MyImportPlugin::log('Successfully saved entry "'.$entry->id.'"', LogLevel::Info);
    };

}

> **Comments:**
> **username_0**: Do I just save this to a .php file and add to plugin folder, or do I just run in a PHP file on its own?  Never build a plugin before.  I appreciate the help Douglas.  
> **username_1**: That question is a bit more complicated. And I guess I assumed that you were already familiar with plugins. This example represents an (incomplete) method in a Service file which would be one part of your plugin. You would call this service method from either an `init` function in your main plugin file; via a Controller method (defined in your plugins controller file that itself would be triggered by hitting a specific url or clicking a button, etc); or from a Variable method (defined in your plugins variable's file and called from a template).  
> **username_1**: I would suggest you start by reading the [plugin documentation](https://buildwithcraft.com/docs/plugins/introduction) and/or installing a plugin template like [BusinessLogic](https://github.com/lindseydiloreto/craft-businesslogic) to get you started. How much data are we talking about? It might be easier to do it manually.  
> **username_0**: 341 entries with about 10 feature fields each.  
> **username_0**: I could merge the 10 fields in the CSV using Excel, but how can I find the array format needed for a Matrix or Table import?  
> **username_1**: That's not a bad option. The best way to find the exact format you need would be to fill in a sample table field the way you want it and then look at the data in the craft database ('craft_content' table) using 'Sequel Pro' or 'phpMyAdmin'. If you want to take a stab at writing a plugin (which is not really that hard), I can probably walk you through as well. Although I might recommend an easier approach, knowing that you already have the data in craft.  
> **username_1**: But table field data in the craft database looks like this (repeated for as many rows as you need): `[{"col1":"myDataTextString","col2":"myDataTextString"},{"col1":"myDataTextString","col2":"myDataTextString"}]`  
> **username_1**: Might also want to do a manual test first before you spend the time reformatting your CSV file (i.e. format one entry in your CSV file manually in the proper craft table format and try and import it using the import plugin).  
> **username_0**: Trying the CSV merge method. Thanks Donald. I really appreciate it.  
> **username_0**: I still need some help with this import if anyone is interested.  My basic issue is this: 1. I have all data in a CSV with about 300 entries 2. I need to merge 10 plain text fields into a Craft Table field 3. I have 5 text fields containing image names that I want to import in Craft Asset field and link to entry ID 4. I have 1 text field containing PDF file name that I want to import into Craft Asset field and link to entry ID  Is anyone able to help with this import?  
> **username_0**: Bob Olde Hampsink helped me sort this one out. Thanks again Bob!  
> **username_0**: Thanks for your help Douglas.  I was able to get some of the data using your method as well.  
> **username_1**: Great! .........  

---

