slamMan Posted October 22, 2009 Share Posted October 22, 2009 I have an array that looks like this: Array ( [0] => Array ( [Customer] => 123 [Contact] => abc [Phone] => 555-555-5555 [Fax] => [Alt. Phone] => [Alt. Contact] => => abc@123.com ) [1] => Array ( [Customer] => 456 [Contact] => def [Phone] => [Fax] => [Alt. Phone] => [Alt. Contact] => => def@456.com ) ) I am having a problem parsing the array into a csv file that would look like this: Customer,Contact,Phone,Fax,Alt. Phone,Alt. Contact,Email 123,abc,555-555-555,,,,abc@123.com 456,def,,,,,def@456.com This is the code I am using: $fp = fopen('imported' . date(mdyHis) . '.csv', 'w'); foreach ($list as $key => $line) { fputcsv($fp, split(',', $line)); } fclose($fp); $list holds the array above. I have tried many variations of the foreach statement and other, but have not found a way to get the output I am looking for. I am sure this is easy and I am missing something. Any help would be appreciated. slamMan Quote Link to comment Share on other sites More sharing options...
slamMan Posted October 22, 2009 Author Share Posted October 22, 2009 Just FYI the file created by this code has 2 rows in it: Array Array I obviously need to go one level further with parsing, but just haven't quite figured it out. Quote Link to comment Share on other sites More sharing options...
salathe Posted October 22, 2009 Share Posted October 22, 2009 Maybe I'm missing something obvious but shouldn't you be using just $line where you currently use split(',', $line)? Quote Link to comment Share on other sites More sharing options...
slamMan Posted October 22, 2009 Author Share Posted October 22, 2009 @salathe I will try that. I added an additional foreach statement and now I get the data in the output file, but it is all in one column and not in rows per record and the headers are not there. I expected the header to be missing since I haven't done anything to extract them yet, but I'm not sure why everything is in cell A1-AXX. Here is the new code $fp = fopen('imported' . date(mdyHis) . '.csv', 'w'); foreach ($list as $array) { foreach ($array as $line) { fputcsv($fp, split(',', $line)); } } fclose($fp); Quote Link to comment Share on other sites More sharing options...
salathe Posted October 22, 2009 Share Posted October 22, 2009 Assuming your array (as shown in the first post) is in $list then the code can be simpler than I think you think it should be. $fp = fopen('imported' . date('mdyHis') . '.csv', 'w'); foreach ($list as $row) { fputcsv($fp, $row); } fclose($fp); P.S. that gives the output (to the file): 123,abc,555-555-5555,,,,abc@123.com 456,def,,,,,def@123.com P.P.S. You could add the headers by doing the following immediately before the foreach loop: fputcsv($fp, array_keys($list[0])); Quote Link to comment Share on other sites More sharing options...
slamMan Posted October 22, 2009 Author Share Posted October 22, 2009 I could have sworn I tried this and it didn't work, but using it the way you have it hear output the two rows of data. Thank you very much!!!! Now if I could just get the field names to show as the column headers it would be perfect. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
slamMan Posted October 23, 2009 Author Share Posted October 23, 2009 @salathe Perfect. Thank you very much. Like you said easier than I was making it. Quote Link to comment Share on other sites More sharing options...
salathe Posted October 23, 2009 Share Posted October 23, 2009 No problem. Remember to click the "solved" button to mark the thread as solved. 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.