0

正規表現を使用してWebページからスクレイピングされた結果セットを含む配列「$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 ファイルに書き込む方法のヒントを教えてください。

4

3 に答える 3

2
$fp = fopen($filename, 'w');
foreach ($results as $row) {
   fputcsv($fp, $row);
}
于 2012-06-03T15:38:24.780 に答える
1

ハイ、

parseCSV クラス、http://www.coursesweb.net/php-mysql/parsecsv_pcを試してください。

CSVデータを簡単に読み込み、2D配列をCSVに変換することもできます。

于 2012-06-03T14:03:47.883 に答える
1

これを使用できます:

$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 ファイルに書き込むには、次のように、結果 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");
于 2012-06-03T14:04:23.973 に答える