简体   繁体   中英

How to traverse an multidimensional array to write it in a delimited file. (PHP)

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.

$fp = fopen($filename, 'w');
foreach ($results as $row) {
   fputcsv($fp, $row);
}

Hy,

Try with the parseCSV class, http://www.coursesweb.net/php-mysql/parsecsv_pc

Can easily read CSV data, also convert 2D array to CSV.

you can use this:

$fp = fopen("file.csv","w");
foreach((array)$results as $val) {
   fwrite($fp,implode(";",$val)."\r\n");
}
fclose($fp);

few things to say:

  • the "\\r\\n" is needed to properly change the line

  • while the most common separator is the comma (,) , i find that some apps like microsoft excel 2010 doesnt like it and instead put the whole line in a cell ; instead the semicolon worked in this case.

  • i always had problems with fputcsv , so instead im going with this.

EDITED:

    $fp = fopen("file.csv","w");
    $contents = file_get_contents('http://www.walmart.com/storeLocator/ca_storefinder_results.do?serviceName=&rx_title=com.wm.www.apps.storelocator.page.serviceLink.title.default&rx_dest=%2Findex.gsp&sfsearch_single_line_address=K6T');
    preg_match_all('/stores\[(\d+)\] \= \{/s', $contents, $matches);        
    foreach ($matches[1] as $index) {       
        preg_match('/stores\[' . $index . '\] \= \{(.*?)\}\;/s', $contents, $matches);
        preg_match_all('/\'([a-zA-Z0-9]+)\' \: ([^\,]*?)\,/s', $matches [1], $matches);
        $c = count ($matches [1]);
        $results = array();
        for ($i=0; $i<$c; $i++)  {
            $results [$matches [1] [$i]] = trim($matches [2] [$i], "\'");
        }
        fwrite($fp,implode(";",array_values($results))."\r\n");
    }
    fclose($fp);

EDITED 2:

to write only specific columns in the .csv file you need to avoid adding them in your results array() OR unset-ting them after, like here:

...
for ($i=0; $i<$c; $i++)  {
    $results [$matches [1] [$i]] = trim($matches [2] [$i], "\'");
}
unset( $results["weekEndSaturday"] );
unset( $results["recentlyOpen"] );
.. go on, renove the non-desired values ..
fwrite($fp,implode(";",array_values($results))."\r\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM