Jump to content

CSV parsing


ju8ular1

Recommended Posts

hi,

iv got a very simple csv parsing problem. im trying to output the first 3 fields on a flat-file database from the 1st record. here's sample data:

2002,Foo,Bar,999,Wages,2000.00,2010.00,2090.00
,,,777,Cellphone,150.00,120.00,150.00
,,,111,Subsidy,400.00,400.00,400.00
1999,Fooz,Bra,999,Wages,2010.00,2320.00,2210.00
,,,777,Cellphone,150.00,120.00,150.00
,,,111,Subsidy,400.00,400.00,400.00

so final output will look like:

2002,Foo,Bar,999,Wages,2000.00,2010.00,2090.00
2002,Foo,Bar,777,Cellphone,150.00,120.00,150.00
2002,Foo,Bar,111,Subsidy,400.00,400.00,400.00
1999,Fooz,Bra,999,Wages,2010.00,2320.00,2210.00
1999,Fooz,Bra,777,Cellphone,150.00,120.00,150.00
1999,Fooz,Bra,111,Subsidy,400.00,400.00,400.00

i know my logic's bent & im getting infinite loops, so i won't even bother posting my code.

any help much appreciated.
Link to comment
Share on other sites

I think I see what you're trying to do - carry on existing data into empty fields until there is data then use that data for more empty fields and so-on.

First use fopen() to open the file, then in a while() loop read each line using fgets(). Use explode() with a "," as a separator to create an array. If the first 3 elements of that array are full store their data. Read the next line, check the first three elements. If empty() use the three stored items and repeat until end of file after which you use fclose() to close the file.

If the data in your CSV file is constant you could store an entire line in an array and use a for() loop to check each element and if one is empty, carry the previous item forward. This would work for all items and not just the first three. Use count() to get the number of elements in an array.
Link to comment
Share on other sites

try

[code]
$filename = 'test01.txt'; // input csv file
$newfile = 'test02.txt';  // output file

$file = file($filename);

$data = array();

foreach ($file as $line) {
         $data[] = explode(',', $line); // make each line an array
}

$k = count($file);
$fp = f open($newfile, 'w');

for ($i=0; $i<$k; $i++) {
     for ($j=0; $j<3; $j++) {
          if ($data[$i][$j]=='') {               // if empty
              $data[$i][$j] = $data[$i-1][$j];   // get from prev item
          }
     }
     f write($fp, join(',', $data[$i]));
}

f close($fp);
[/code]
Link to comment
Share on other sites

thanks barand, that worked fine.

a few questions:
-file() reads in entire contents into array. how does PHP differentiate between every $line of $data?
-similarly, how does count($data) count only the number of delimited variables on current line, instead of the whole file?
Link to comment
Share on other sites

[!--quoteo(post=364903:date=Apr 14 2006, 10:17 PM:name=ju8ular1)--][div class=\'quotetop\']QUOTE(ju8ular1 @ Apr 14 2006, 10:17 PM) [snapback]364903[/snapback][/div][div class=\'quotemain\'][!--quotec--]
a few questions:
-file() reads in entire contents into array. how does PHP differentiate between every $line of $data?
-similarly, how does count($data) count only the number of delimited variables on current line, instead of the whole file?
[/quote]

File() looks for newline characters (\n) and puts each line in an array element.

count($data) in the above code would give the same value as count($file).

count($data[$n]) would count the number of items on line $n since each line was converted to an array.

[a href=\"http://www.php.net/count\" target=\"_blank\"]count() function[/a]
Link to comment
Share on other sites

i get it now. thanks a lot. um, iv got a similar problem. file format is:

2002,Foo,Bar,21 Childers Crs, VA, USA
2002,Foo,Bar,16 Killkenny Rd,VA,USA
2002,Foo,Bar,417 Junk Ave,MA,USA
1999,Fooz,Bra,3207 Slimjims Dr,WI,USA
1999,Fooz,Bra,13 Ungry Pl,WI,USA

aim is to print out the last line of each unique record. i know you can do this in SQL, like:
[code]
SQL>SELECT field1, max(*) FROM table GROUP BY field1
[/code][code]

how do u do this in PHP?

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.