Jump to content

Tab delimited file into Array Structure


michaelham

Recommended Posts

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. :confused:

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.