search
Homephp教程php手册php array数组生成csv文件

在php中生成csv文件的方法很是简单,这里我来介绍一下自己用到的一个实例,把数组直接转换成csv文件输出,有需要了解的朋友可参考,代码如下:

<?php  
$data = array(  
    array( &#39;row_1_col_1&#39;, &#39;row_1_col_2&#39;, &#39;row_1_col_3&#39; ),  
    array( &#39;row_2_col_1&#39;, &#39;row_2_col_2&#39;, &#39;row_2_col_3&#39; ),  
    array( &#39;row_3_col_1&#39;, &#39;row_3_col_2&#39;, &#39;row_3_col_3&#39; ),  
);  
$filename = "example";  
header("Content-type: text/csv");  
header("Content-Disposition: attachment; filename={$filename}.csv");  
header("Pragma: no-cache");  
header("Expires: 0"); 
outputCSV($data);


function outputCSV($data) {  
    $outputBuffer = fopen("php://output", &#39;w&#39;);  
    foreach($data as $val) {  
    foreach ($val as $key => $val2) {  
     $val[$key] = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $val2);// CSV的Excel支持GBK编码,一定要转换,否则乱码  
     }  
        fputcsv($outputBuffer, $val);  
    }  
    fclose($outputBuffer);  
}

例2,看一个读取csv文件的实例,读取cvs,使用fgetcsv()文件指针中读入一行并解析 CSV 字段,比如说我要读取如下csv文件,代码如下:

<?php 
/** by www.phprm.com */ 
$row = 1; 
$handle = fopen("file.csv","r"); 
//fgetcsv() 解析读入的行并找出 CSV格式的字段然后返回一个包含这些字段的数组。  
while ($data = fgetcsv($handle, 1000, ",")) { 
$num = count($data); 
echo "<p> $num fields in line $row: <br>\n"; 
$row++; 
for ($c=0; $c < $num; $c++) { 
    //注意中文乱码问题 
    $data[$c]=iconv("gbk", "utf-8//IGNORE",$data[$c]);   
        echo $data[$c] . "<br>\n"; 
    } 
} 
fclose($handle);


文章地址:

转载随意^^请带上本文地址!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!