簡體   English   中英

如何遍歷多維數組以將其寫入帶分隔符的文件中。 (PHP)

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

我有一個數組“ $ results”,其中包含結果集,該結果集是使用正則表達式從網頁中抓取的。 在遍歷數組以將數組的整個數據寫入.csv文件時,我有些困惑。

這是按以下方式打印后的數組輸出。

print_r ($results);    //printing the array

產量

 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
    )

我只使用簡單的數組,任何人都可以提示我如何遍歷$results數組以將其寫入.csv文件或.xls文件中。

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

HY,

嘗試使用parseCSV類http://www.coursesweb.net/php-mysql/parsecsv_pc

可以輕松讀取CSV數據,也可以將2D數組轉換為CSV。

您可以使用此:

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

幾句話要說:

  • 需要"\\r\\n"才能正確更改行

  • 雖然最常見的分隔符是逗號(,) ,但我發現某些應用程序(例如microsoft excel 2010)不喜歡它,而是put the whole line in a cell 相反,在這種情況下, semicolon起作用了。

  • 我一直對fputcsv有問題,所以我直接與此有關。

編輯:

    $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);

編輯2:

only specific columns在.csv文件中寫入only specific columns ,您需要避免將它們添加到結果array() 在之后取消設置,例如:

...
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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM