scott.stephan Posted June 14, 2009 Share Posted June 14, 2009 Basically, I need a lead row on my .csv file. I created the string, I created the putfile. Everything runs perfectly. EXCEPT, no matter how I do, the lead row is ALWAYS the last row in the file. Does fputcsv run BACKWARDS? Here's the code: $leadrow="ORDER,DATE,TYPE,PRIORITY,PO,EMAIL,SHIPVIA,SHIPAFTER,CUSTOMERID,BILLTONAME,BILLTOSTREET,BILLTOSTREET2,BILLTOCITY,BILLTOSTATE,BILLTOZIP,BILLTOCOUNTRY,BILLTOCONTACT,SHIPTONAME,SHIPTOSTREET,SHIPTOSTREET2,SHIPTOCITY,SHIPTOSTATE,WHIPTOZIP,SHIPTOCOUNTRY,SHIPTOCONTACT,SHIPTOPHONE,SKU,DESC,QTYORDERED,QTYSHIPPED,QTYBACKORDERED,3RDPARTYSHIP,3RDPARTACCT,NOTES"; $list[0]=$leadrow; $fp = fopen('TodfdsdfdfCO3.csv', 'w'); foreach ($list as $line) { fputcsv($fp, split(',', $line)); } fclose($fp); And the output is the contents of $list followed by the lead row in dead last place, even though it should be in position 0. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/162082-php-to-csv-fput-running-backwards/ Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 I assume $list is a built up array then just before you output your adding $list[0]=$leadrow; have you tried adding $list[0]=$leadrow; before building the array ? Quote Link to comment https://forums.phpfreaks.com/topic/162082-php-to-csv-fput-running-backwards/#findComment-855283 Share on other sites More sharing options...
scott.stephan Posted June 14, 2009 Author Share Posted June 14, 2009 Awesome. That did it! What's the reason here? Quote Link to comment https://forums.phpfreaks.com/topic/162082-php-to-csv-fput-running-backwards/#findComment-855312 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 The array elements are added in the order that the code adds them and that is the order that the foreach() loop operates on them. There is nothing magic about an array index [0] unless you sort the array or iterate over the array using ordered numeric index values. Quote Link to comment https://forums.phpfreaks.com/topic/162082-php-to-csv-fput-running-backwards/#findComment-855316 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 Yep what PFMaBiSmAd said, a trick i have used in before is to set $list[0]=""; at the start then when i get the details i set it again $list[0]=$leadrow, the first one makes sure its in the correct place, of course you could sort it Quote Link to comment https://forums.phpfreaks.com/topic/162082-php-to-csv-fput-running-backwards/#findComment-855324 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.