php csv to array (csv 转数组)
2011-11-29 22:45
351 查看
php csv to array (csv 转数组)
1. 使用类注意: user.csv 第一行默认为数组的键, 且第一行不会被打印; (区别于下面的普通函数方法)
例如:
name | age | gender |
zhang | 23 | male |
li | 20 | female |
Php代码
Array
(
[0] => Array
(
[name] => zhang
[age] => 23
[gender] => male
)
[1] => Array
(
[name] => li
[age] => 20
[gender] => female
)
)
回到正题:
Php代码
<?php
class csv_to_array{
private $counter;
private $handler;
private $length;
private $file;
private $seprator;
private $csvData = array();
function __construct($file = "", $length = 1000, $seprator = ','){
$this->counter = 0;
$this->length = $length;
$this->file = $file;
$this->seprator = $seprator;
$this->handler = fopen($this->file, "r");
}
function get_array(){
$getCsvArr = array();
$csvDataArr = array();
while(($data = fgetcsv($this->handler, $this->length, $this->seprator)) != FALSE){
$num = count($data);
$getCsvArr[$this->counter] = $data;
$this->counter++;
}
if(count($getCsvArr) > 0){
$csvDataArr = array_shift($getCsvArr);
$counter = 0;
foreach($getCsvArr as $csvValue){
$totalRec = count($csvValue);
for($i = 0; $i < $totalRec ; $i++) $this->csvData[$counter][$csvDataArr[$i]] = $csvValue[$i];
$counter++;
}
}
return $this->csvData;
}
function __destruct(){
fclose($this->handler);
}
}
// example:
$csv = new csv_to_array('user.csv');
echo "<pre>"; print_r($csv->get_array()); echo "</pre>";
2. 使用一般函数
Php代码
<?
function csv_to_array($csv)
{
$len = strlen($csv);
$table = array();
$cur_row = array();
$cur_val = "";
$state = "first item";
for ($i = 0; $i < $len; $i++)
{
//sleep(1000);
$ch = substr($csv,$i,1);
if ($state == "first item")
{
if ($ch == '"') $state = "we're quoted hea";
elseif ($ch == ",") //empty
{
$cur_row[] = ""; //done with first one
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\n")
{
$cur_row[] = $cur_val;
$table[] = $cur_row;
$cur_row = array();
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\r") $state = "wait for a line feed, if so close out row!";
else
{
$cur_val .= $ch;
$state = "gather not quote";
}
}
elseif ($state == "we're quoted hea")
{
if ($ch == '"') $state = "potential end quote found";
else $cur_val .= $ch;
}
elseif ($state == "potential end quote found")
{
if ($ch == '"')
{
$cur_val .= '"';
$state = "we're quoted hea";
}
elseif ($ch == ',')
{
$cur_row[] = $cur_val;
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\n")
{
$cur_row[] = $cur_val;
$table[] = $cur_row;
$cur_row = array();
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\r") $state = "wait for a line feed, if so close out row!";
else
{
$cur_val .= $ch;
$state = "we're quoted hea";
}
}
elseif ($state == "wait for a line feed, if so close out row!")
{
if ($ch == "\n")
{
$cur_row[] = $cur_val;
$cur_val = "";
$table[] = $cur_row;
$cur_row = array();
$state = "first item";
}
else
{
$cur_row[] = $cur_val;
$table[] = $cur_row;
$cur_row = array();
$cur_val = $ch;
$state = "gather not quote";
}
}
elseif ($state == "gather not quote")
{
if ($ch == ",")
{
$cur_row[] = $cur_val;
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\n")
{
$cur_row[] = $cur_val;
$table[] = $cur_row;
$cur_row = array();
$cur_val = "";
$state = "first item";
}
elseif ($ch == "\r") $state = "wait for a line feed, if so close out row!";
else $cur_val .= $ch;
}
}
return $table;
}
//pass a csv string, get a php array
// example:
$arr = csv_to_array(file_get_contents('user.csv'));
echo "<pre>"; print_r($arr); echo "</pre>"
或者
Php代码
<?
$arrCSV = array();
// Open the CSV
if (($handle = fopen("user.csv", "r")) !==FALSE) {
// Set the parent array key to 0
$key = 0;
// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
// Count the total keys in each row
$c = count($data);
//Populate the array
for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];
$key++;
} // end while
// Close the CSV file
fclose($handle);
} // end if
echo "<pre>"; print_r($arrCSV); echo "</pre>";
?>
相关文章推荐
- php csv to array (csv 转数组)
- php 对象转数组 objectToArray
- PHP XML To Array将XML转换为数组
- Php的数组转换成XML(php Array to Xml)
- PHP XML To Array,将XML转换为数组
- PHP SOAP client pass array to C# SOAP webservice(PHP SOAP 数组参数传递)
- php数组转换为对象PHP - Convert Array to Object with stdClass
- PHP XML To Array,将XML转换为数组
- php数组转换为对象PHP - Convert Array to Object with stdClass
- php计算数组相同值出现次数的代码(array_count_values)
- PHP 数组遍历 的差异(array_diff 的实现)
- php从数组中取出一段 之 array_slice
- 如何使用List.toArray()转换为数组对象
- list.toArray()转换为数组对象
- PHP数组array_multisort排序详解
- PHP合并数组array_merge函数运算符加号与的区别
- php读取csv数据保存到数组代码
- php 利用array_slice函数获取随机数组或前几条数据
- Leetcode 108 Convert Sorted Array to Binary Search Tree 有序数组构造平衡二叉查找树
- php 利用array_slice函数获取随机数组或前几条数据