michaelham Posted July 29, 2009 Share Posted July 29, 2009 I have a tab delimited file that I will be using to update a database. The file has the column names in the first row and the records following. Is there a way I can retrieve the records into an array and access it similar to the way mysql_fetch_array does it. For example My file looks like this LastName<tab>FirstName<tab>PhoneNumber<tab>ZipCode Smith<tab>Bob<tab>5555555<tab>11212 Thomas<tab>James<tab>6562432<tab>11929 I would like to be able to loop through one record at a time and reference the array like this while($row = NEXT RECORD SOME HOW) { echo "<br />Last Name = " . $row['LastName'] . " FirstName = " . $row['FirstName']; } and have it display Last Name = Smith First Name = Bob Last Name = Thomas First Name = James Any ideas? I would like to be able to do this because my actual files each have about 216 columns and that one is for a small table and the primary key is somewhere in the middle but is always called ListingNumber and the location changes depending on which table it refers to. Quote Link to comment Share on other sites More sharing options...
gevans Posted July 29, 2009 Share Posted July 29, 2009 <?php $lines = file('your-file'); foreach($lines as $line_number => $line) { $details = explode("<tab>", $line); echo "<pre>",var_dump($details),"</pre> } ?> Have a play with that. Just get your file name in place and see what gets printed from var_dump. Quote Link to comment Share on other sites More sharing options...
patrickmvi Posted July 30, 2009 Share Posted July 30, 2009 Your explode statement would be explode("\t", $line) instead of explode("<tab>", $line). Quote Link to comment Share on other sites More sharing options...
michaelham Posted July 30, 2009 Author Share Posted July 30, 2009 I am getting the file read in with that but it is still indexing each record with 0,1,2,3,4,5 instead of the column names. Quote Link to comment Share on other sites More sharing options...
gevans Posted July 30, 2009 Share Posted July 30, 2009 Could you give me an example of your output, I don't fully understand Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 You're better off leaving the rows numerically indexed; otherwise you'll have to loop over the columns in a row more than once. When you say update a database, do you mean you are inserting or are you inserting and updating duplicate records? Also, which database are you talking about here? Many databases provide a way to bulk insert records. In that case it's often easier to bulk insert into a temporary table and then perform the updates with a couple of SQL statements rather than writing a PHP script. Quote Link to comment Share on other sites More sharing options...
michaelham Posted July 30, 2009 Author Share Posted July 30, 2009 I have figure out a way to get ahold of the index I am looking for during the first iteration. Then I can always access the value I need. The script will actually be doing both inserting new record and updating old (by deleting the old record and reinserting with the new/updated record). I have this working but I now have a different issue, but it isn't related to this so I will open a new thread. Please look for it, called PHP CLI Problem. Thanks Thanks everybody for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.