ju8ular1 Posted April 14, 2006 Share Posted April 14, 2006 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.001999,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.00so final output will look like:2002,Foo,Bar,999,Wages,2000.00,2010.00,2090.002002,Foo,Bar,777,Cellphone,150.00,120.00,150.002002,Foo,Bar,111,Subsidy,400.00,400.00,400.001999,Fooz,Bra,999,Wages,2010.00,2320.00,2210.001999,Fooz,Bra,777,Cellphone,150.00,120.00,150.001999,Fooz,Bra,111,Subsidy,400.00,400.00,400.00i know my logic's bent & im getting infinite loops, so i won't even bother posting my code.any help much appreciated. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 14, 2006 Share Posted April 14, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 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] Quote Link to comment Share on other sites More sharing options...
ju8ular1 Posted April 14, 2006 Author Share Posted April 14, 2006 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 [!--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] Quote Link to comment Share on other sites More sharing options...
ju8ular1 Posted April 15, 2006 Author Share Posted April 15, 2006 i get it now. thanks a lot. um, iv got a similar problem. file format is:2002,Foo,Bar,21 Childers Crs, VA, USA2002,Foo,Bar,16 Killkenny Rd,VA,USA2002,Foo,Bar,417 Junk Ave,MA,USA1999,Fooz,Bra,3207 Slimjims Dr,WI,USA1999,Fooz,Bra,13 Ungry Pl,WI,USAaim 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? 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.