How to dynamically create Drupal 7 tables based on form API input values? I have an array with the data below:

arr_fields[0] = 'a bigint'
arr_fields[1] = 'b bit'


I want to structure it just like the array structure below:

$fields = array(
            'a' =&gt; array(
                'type' =&gt; 'bigint',             
            ),
            'b' =&gt; array(
                'type' =&gt; 'bit',                
            )
);


Below is the rest of the code and it's working fine.

$schema = array(
    'description' =&gt; t ('Contains a particular templates data inputted from the MyID Inputting System'),
    'fields' =&gt; $fields,
);

db_create_table('myid_template_' . $template_id, $schema);


I tried the code below to structure it like the above, but it throws an error

foreach ($arr_fields as $value) {
    $arr = explode(' ' , $value);
    $fields[$arr[0]] = array('type' =&gt; $arr[1]);
}

$schema = array(
    'description' =&gt; t ('Contains a particular templates data inputted from the MyID Inputting System'),
    'fields' =&gt; $fields,
);

db_create_table('myid_template_' . $template_id, $schema);


An error says:

&lt;em class="placeholder"&gt;PDOException&lt;/em&gt;: SQLSTATE[42601]: Syntax error:  7 ERROR:  syntax error at end of input
LINE 4: )
     ^: CREATE TABLE {myid_template_489} (
a ,
b 
); Array
(
)
in &lt;em class="placeholder"&gt;db_create_table()&lt;/em&gt; (line &lt;em class="placeholder"&gt;2720&lt;/em&gt; of &lt;em class="placeholder"&gt;C:\xampp\htdocs\drupal-7.34\includes\database\database.inc&lt;/em&gt;).


How should I structure it?

foreach ($arr_fields as $value) {
    $arr = explode(' ' , $value);
    $fields[$arr[0]] = array('type' =&gt; $arr[1]);
}


UPDATE 1:

Here's my form input pass through AJAX POST method:
As I read the output of the error message, I suppose the values you are trying to parse do not contain the type of the database field.

Did you check your PHP error log as well? There might be PHP warnings, stating that $arr[1] is not set.

Verify your parsed values and make sure they contain the type information.

An additional note: I do not know your special use case. But creating a new table on each template might easily bloat your database structure. Maybe you find a better solution to store your information within one dedicated table, using your template ID as identifier and serialized arrays holding the information?