Hello all. Was looking for a little help. I have a CSV file that looks like this: ,,,,,,,,, ,,,,,,,,, "UNIT A ""1/1/2016"" to ""1/17/2016"", 0 (activity level)",,,,,,,,, Description,Quantity,Weight,Cost,Daily Qty,Daily Wgt,Daily Cst,C Qty,C Wgt,C Cst UNIT A,"6,193","5,268 Lb",$ 0.00,364.29,310 Lb,$ 0.00,N/A,N/A,N/A 1010 ,"1,356","1,464 Lb",$ 0.00,79.76,86 Lb,$ 0.00,N/A,N/A,N/A 1050 ,119,95 Lb,$ 0.00,7.00,6 Lb,$ 0.00,N/A,N/A,N/A 1210 ,449,534 Lb,$ 0.00,26.41,31 Lb,$ 0.00,N/A,N/A,N/A 1310 ,597,131 Lb,$ 0.00,35.12,8 Lb,$ 0.00,N/A,N/A,N/A 1810 ,"1,499",125 Lb,$ 0.00,88.18,7 Lb,$ 0.00,N/A,N/A,N/A 1710 ,"1,050",483 Lb,$ 0.00,61.76,28 Lb,$ 0.00,N/A,N/A,N/A 1510 ,167,334 Lb,$ 0.00,9.82,20 Lb,$ 0.00,N/A,N/A,N/A 1410 ,471,"1,696 Lb",$ 0.00,27.71,100 Lb,$ 0.00,N/A,N/A,N/A 2310 ,369,258 Lb,$ 0.00,21.71,15 Lb,$ 0.00,N/A,N/A,N/A 2320 ,116,147 Lb,$ 0.00,6.82,9 Lb,$ 0.00,N/A,N/A,N/A ,,,,,,,,, ,,,,,,,,, "UNIT B""1/1/2016"" to ""1/17/2016"", 0 (activity level)",,,,,,,,, Description,Quantity,Weight,Cost,Daily Qty,Daily Wgt,Daily Cst,C Qty,C Wgt,C Cst UNIT B,"6,193","5,268 Lb",$ 0.00,364.29,310 Lb,$ 0.00,N/A,N/A,N/A 1010 ,"1,356","1,464 Lb",$ 0.00,79.76,86 Lb,$ 0.00,N/A,N/A,N/A 1050 ,119,95 Lb,$ 0.00,7.00,6 Lb,$ 0.00,N/A,N/A,N/A 1210 ,449,534 Lb,$ 0.00,26.41,31 Lb,$ 0.00,N/A,N/A,N/A 1310 ,597,131 Lb,$ 0.00,35.12,8 Lb,$ 0.00,N/A,N/A,N/A 1810 ,"1,499",125 Lb,$ 0.00,88.18,7 Lb,$ 0.00,N/A,N/A,N/A 1710 ,"1,050",483 Lb,$ 0.00,61.76,28 Lb,$ 0.00,N/A,N/A,N/A 1510 ,167,334 Lb,$ 0.00,9.82,20 Lb,$ 0.00,N/A,N/A,N/A 1410 ,471,"1,696 Lb",$ 0.00,27.71,100 Lb,$ 0.00,N/A,N/A,N/A 2310 ,369,258 Lb,$ 0.00,21.71,15 Lb,$ 0.00,N/A,N/A,N/A 2320 ,116,147 Lb,$ 0.00,6.82,9 Lb,$ 0.00,N/A,N/A,N/A and so on.... I am looking to format this data into something like this : "UNITID","PRODUCTID","QTY" For each item listed on under that unit block. So it would basically have to look for the : ,,,,,,,,, ,,,,,,,,, To know that it's a new unit. Then get the first column of the third row within a block, "Unit X", and use it as UNITID, that is to be used for each following rows for unit identification, and then just grab the first and second column of each row for the prodid and qty columns. Any help would be greatly appreciated. Thanks!
I wouldn't make any changes to the file just import it, and then manage the data from within your php script. No need to create extra steps. see: http://php.net/manual/en/function.str-getcsv.php
@sarahk I am looking to parse the data I am needing, as another array will be created to import into a DB. I was just thinking that a function that would loop through each block and then add the data I am looking for to an array, I thought something like that would be a lot easier. Unfortunately my programming skills are not up to par with me thinking skills.
It's the double handling I'm tying to avoid I'd just import into Excel, get rid of the columns you don't want and save again as a .csv
Note: in following colums are 0 based indexed, that is first column will be 0, second will be 1, 3rd will be 2 and so on. If you need selective columns, us this code: $c = fopen("myfile.csv", "rb"); while (($row = fgetcsv($c, 2048) !== FALSE) { print $row[0] ."<br/>"; //0 = first column. print first column of each row //and so on check all columns suiting your needs and //insert to database } fclose($c); Code (markup): I hope it helps. Stay well.....