Note that there are some explanatory texts on larger screens.

plurals
  1. POExplode entire array, alter all values in some columns then implode back to old array
    text
    copied!<p>I have an array, which contain lines from a txt file which I need to insert into a sql table, however 4 of the 24 columns (all values /t spaced) are dates but with time etc on the end so all need to be shortened by 15 characters so substr. </p> <p>So I've tried numerous methods to alter the values in the file then insert them, but it either just doesn't seem to work like my current code:</p> <pre><code>$lines = file('F:/xamptest/htdocs/UniProject/upload/Amazon output.txt'); foreach($arr as $line) { $arr1 = explode("\t", $line); $arr1[2] = substr($arr1[2], 0, -15); $arr1[3] = substr($arr1[3], 0, -15); $arr1[4] = substr($arr1[4], 0, -15); $arr1[5] = substr($arr1[5], 0, -15); $arri = implode("\t",$arr1); file_put_contents('upload/ImportLine.txt', $arri); mysql_query ("LOAD DATA INFILE 'F:/xamptest/htdocs/UniProject/upload/Amazon output.txt' INTO TABLE imported_orders FIELDS TERMINATED BY '\t'", $conn); } </code></pre> <p>Which should alter the dates in the line save it to a txt file then insert it to the table and loop round for each row. But all this does is insert the last line of the file, so its looped through but then just overwrites itself. Or my methods are far too long winded and involve having to tables and a massive sql insert listing all 24 of the columns and values. </p> <p><strong>My Point</strong></p> <p>So I was wondering if I could in a way explode the entire array which contains the file lines and mass substr from the dates, then restore it back to the old array lines format put that back in the file and just LOAD DATA INFILE from that. As loading the original file works, but the dates can't be as date type because they're "2013-03-11T20:35:33+00:00" for example.</p> <p>Also if there is a way to simply insert a variable which as /t deliberated values inside without having to make it an array and state eachColumn=andValue[2] that'd be even better.</p> <p>Many thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload