您好,欢迎访问一九零五行业门户网

php csv to array(csv 转数组)方法与代码

counter = 0; $this->length = $length; $this->file = $file; $this->seprator = $seprator; $this->specialhtml = $specialhtml; $this->csvdataarr = is_array($csvdataarr) ? $csvdataarr : array(); $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); if($this->csvdataarr) $csvdataarr = $this->csvdataarr; $counter = 0; foreach($getcsvarr as $csvvalue) { $totalrec = count($csvvalue); for($i = 0; $i { $key = $this->csvdataarr ? $csvdataarr[$i] : $this->remove_char($csvdataarr[$i]); if($csvvalue[$i]) $this->csvdata[$counter][$key] = $this->put_special_char($csvvalue[$i]); } $counter++; } } return $this->csvdata; } function put_special_char($value) { return $this->specialhtml ? str_replace(array('&',' ','\'',''),array('&','',''','<','>'),$value) : $value; } function remove_char($value) { $result = $this->removechar == 'manual' ? $this->remove_char_manual($value) : $this->remove_char_auto($value); return str_replace(' ','_',trim($result)); } private function remove_char_manual($value) { return str_replace(array('&','','\'','','(',')','%'),'',trim($value)); } private function remove_char_auto($str,$x=0) { $x==0 ? $str=$this->make_semiangle($str) : '' ; eregi('[[:punct:]]',$str,$arr); $str = str_replace($arr[0],'',$str); return eregi('[[:punct:]]',$str) ? $this->remove_char_auto($str,1) : $str; } private function make_semiangle($str) { $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[', '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']', '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => ' '》' => '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-', ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', '”' => '', '’' => '`', '‘' => '`', '|' => '|', '〃' => '', ' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*'); return strtr($str, $arr); } function __destruct(){ fclose($this->handler); } } // example: $csv = new csv_to_array('user.csv'); echo ; print_r($csv->get_array()); echo
; ?>
复制代码
2、使用一般函数
function csv_to_array($csv) { $len = strlen($csv); $table = array(); $cur_row = array(); $cur_val = ; $state = first item; for ($i = 0; $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 ; print_r($arr); echo
?>
复制代码
或者
$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 $key++; } // end while // close the csv file fclose($handle); } // end if echo ; print_r($arrcsv); echo
; ?>
复制代码
至于哪种更好用,看自己的实际需求与个人爱好了,实际工作中csv转array的需求还是不少,建议大家多练习,多掌握。
其它类似信息

推荐信息