PHP Convert array into csv
2013-12-11 17:34
337 查看
My solution requires the array be formatted differently than provided in the question:
We define our function:
Then we output our data as a CSV:
I have used this with several projects, and it works well. I should note that the outputCSV code is more clever than I am, so I am sure I am not the original author. Unfortunately I have lost track of where I got it, so I can't give the credit to whom it is due.
<? $data = array( array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ), array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ), array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ), ); ?>
We define our function:
<? function outputCSV($data) { $outputBuffer = fopen("php://output", 'w'); foreach($data as $val) { fputcsv($outputBuffer, $val); } fclose($outputBuffer); } ?>
Then we output our data as a CSV:
<? $filename = "example"; header("Content-type: text/csv"); header("Content-Disposition: attachment; filename={$filename}.csv"); header("Pragma: no-cache"); header("Expires: 0"); outputCSV($data); ?>
I have used this with several projects, and it works well. I should note that the outputCSV code is more clever than I am, so I am sure I am not the original author. Unfortunately I have lost track of where I got it, so I can't give the credit to whom it is due.
相关文章推荐
- Insight into DOMDocument - how to convert data from XML to array in PHP
- How to convert CSV into Database Table?
- Convert sorted array into binary search tree
- php Split an Array into N Arrays
- Convert dictionary values into array
- php json array convert 嵌套 转换 操作
- convert an int into a byte array
- php csv to array (csv 转数组)
- Passing PHP array into external Javascript function as array
- php数组转换为对象PHP - Convert Array to Object with stdClass
- 'Could not convert variant of type (Array Byte) into type (Integer)
- php数组转换为对象PHP - Convert Array to Object with stdClass
- php csv to array (csv 转数组)
- convert a sorted array into a balanced binary search tree
- Convert Sorted Array to Binary Search Tree
- PHP实现客户端将CSV文件导入服务器数据库中
- 基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
- 【php数组 函数详解】array_change_key_case将数组的键转换为大写或小写
- php读取csv数据保存到数组的方法