db_insert a lot of records I am struggling to sort out how to insert a lot of records using db_insert

Please see below for the code.

I can't have a for loop in the array. It breaks but I am dealing with looping through a lot of data. 48 (states) X 365 (# days) X 36 years. 

What is the best way of accomplishing this?

Thanks!

$query = db_insert('HDD')
  -&gt;fields(array(
  for ($s = 0; $s &lt;= $cnt; $s++) {
    'HDDDAYS' =&gt; $time, //Loops through the dates
    'AL'=&gt; $output1[$sts[0]][$dts[$s]],
    'AR'=&gt; $output1[$sts[1]][$dts[$s]],
    'AZ'=&gt; $output1[$sts[2]][$dts[$s]],
    'CA'=&gt; $output1[$sts[3]][$dts[$s]],
    'CO'=&gt; $output1[$sts[4]][$dts[$s]],
    'CT'=&gt; $output1[$sts[5]][$dts[$s]],
    'DE'=&gt; $output1[$sts[6]][$dts[$s]],
    'FL'=&gt; $output1[$sts[7]][$dts[$s]],
    ....
    'WI'=&gt; $output1[$sts[45]][$dts[$s]],
    'WV'=&gt; $output1[$sts[46]][$dts[$s]],
    'WY'=&gt; $output1[$sts[47]][$dts[$s]]
  }));
  $query-&gt;execute();
You could define your query fields first, then set your field values in a loop before executing the query.

Example (not tested):

$query = db_insert('HDD')-&gt;fields(array(
  'HDDDAYS',
  'AL',
  'AR',
  ...
));

for ($s = 0; $s &lt;= $cnt; $s++) {
  $query-&gt;values(array(
    'HDDDAYS' =&gt; $time,
    'AL' =&gt; $output1[$sts[0]][$dts[$s]],
    'AR' =&gt; $output1[$sts[1]][$dts[$s]],
    ...
  ));
}

$query-&gt;execute();