php array数组生成csv文件
在php中生成csv文件的方法很是简单,这里我来介绍一下自己用到的一个实例,把数组直接转换成csv文件输出,有需要了解的朋友可参考,代码如下:
<?php $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' ), ); $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", 'w'); foreach($data as $val) { foreach ($val as $key => $val2) { $val[$key] = iconv('utf-8', 'gbk', $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);
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
