I’ve an array “$results” that contains result-set which is scraped from web page using regular expressions.
I’m bit confused in traversing the array to write the whole data of array in .csv file.
here is output of array after printing it in following way.
print_r ($results); //printing the array
Output
Array
(
[fullName] => Ogdensburg Walmart Store #2092
[street1] => 3000 Ford Street Ext
[city] => Ogdensburg
[state] => NY
[zipcode] => 13669
[phone] => (315) 394-8990
[latitude] => 44.7083
[longitude] => -75.4564
)
Array
(
[fullName] => Evans Mills Walmart Supercenter Store #5497
[street1] => 25737 Us Route #11
[city] => Evans Mills
[state] => NY
[zipcode] => 13637
[phone] => (315) 629-2124
[latitude] => 44.0369
[longitude] => -75.8455
)
Array
(
[fullName] => Watertown Walmart Supercenter Store #1871
[street1] => 20823 State Route 3
[city] => Watertown
[state] => NY
[zipcode] => 13601
[phone] => (315) 786-0145
[latitude] => 43.9773
[longitude] => -75.9579
)
I’ve only worked with simple arrays, can anybody give me hint how to traverse $results array to write it in .csv file or .xls file.
you can use this:
few things to say:
the
"\r\n"is needed to properly change the linewhile the most common separator is the comma
(,), i find that someapps like microsoft excel 2010 doesnt like it and instead
put the whole line in a cell; instead thesemicolonworked in this case.i always had problems with fputcsv, so instead im going with this.
EDITED:
EDITED 2:
to write
only specific columnsin the .csv file you need to avoid adding them in yourresults array() OR unset-ting them after, like here: