Jump to content


Photo

CSV parsing


  • Please log in to reply
5 replies to this topic

#1 ju8ular1

ju8ular1
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 14 April 2006 - 06:05 AM

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.

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 14 April 2006 - 08:55 AM

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.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 April 2006 - 09:58 AM

try

$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);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 ju8ular1

ju8ular1
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 14 April 2006 - 09:17 PM

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?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 April 2006 - 10:04 PM

[!--quoteo(post=364903:date=Apr 14 2006, 10:17 PM:name=ju8ular1)--][div class=\'quotetop\']QUOTE(ju8ular1 @ Apr 14 2006, 10:17 PM) View Post[/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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 ju8ular1

ju8ular1
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 15 April 2006 - 03:15 AM

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:
SQL>SELECT field1, max(*) FROM table GROUP BY field1
[code=auto:0]

how do u do this in PHP?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users