Created
May 25, 2014 06:06
-
-
Save masarufuruya/52d922d2f36287dbe345 to your computer and use it in GitHub Desktop.
PHPで連想配列を日本語エンコードしてCSV出力する ref: http://qiita.com/masarufuruya/items/e5e348ae325740c49795
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$data = array( | |
array('佐藤', '東京都', '29歳'), | |
array('田中', '千葉県', '31歳'), | |
array('鈴木', '北海道', '54歳') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$data = array( | |
array('佐藤', '東京都', '29歳'), | |
array('田中', '千葉県', '31歳'), | |
array('鈴木', '北海道', '54歳') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$assoc_data = array( | |
array('name' => '佐藤', 'address' => '東京都', 'old' => '29歳'), | |
array('name' => '田中', 'address' => '千葉県', 'old' => '31歳'), | |
array('name' => '鈴木', 'address' => '北海道', 'old' => '54歳') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$assoc_data = array( | |
array('name' => '佐藤', 'address' => '東京都', 'old' => '29歳'), | |
array('name' => '田中', 'address' => '千葉県', 'old' => '31歳'), | |
array('name' => '鈴木', 'address' => '北海道', 'old' => '54歳') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csv_header = array( | |
'name', | |
'address', | |
'old', | |
); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename=data.csv'); | |
$stream = fopen('php://output', 'w'); | |
fputcsv($stream, $csv_header); | |
foreach($assoc_data as $key => $assoc_row){ | |
$numeric_row = array(); | |
foreach ($csv_header as $header_name) { | |
mb_convert_variables('SJIS-win', 'UTF-8', $assoc_row[$header_name]); | |
$numeric_row[] = $assoc_row[$header_name]; | |
} | |
fputcsv($stream, $numeric_row); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csv_header = array( | |
'name', | |
'address', | |
'old', | |
); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename=data.csv'); | |
$stream = fopen('php://output', 'w'); | |
fputcsv($stream, $csv_header); | |
foreach($assoc_data as $key => $assoc_row){ | |
$numeric_row = array(); | |
foreach ($csv_header as $header_name) { | |
mb_convert_variables('SJIS-win', 'UTF-8', $assoc_row[$header_name]); | |
$numeric_row[] = $assoc_row[$header_name]; | |
} | |
fputcsv($stream, $numeric_row); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mb_convert_encording |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment