- Ini_set (' Memory_limit ', '-1 '); If the CSV is larger, you can add them.
- /*
- * $file: CSV file
- * $CSVDATAARR: Header of CSV table, eg:arary (' name ', ' sex ', ' age ') or array (0,1,2)
- * $specialhtml: Whether does want to convert special characters to HTML entities?
- * $removechar: Which type does want to remove special characters in array keys, manual or Automatical?
- * Edit http://bbs.it-home.org
- */
- Class Csv_to_array
- {
- Private $counter;
- Private $handler;
- Private $length;
- Private $file;
- Private $seprator;
- Private $specialhtml;
- Private $removechar = ' manual ';
- Private $CSVDATAARR;
- Private $csvData = Array ();
- function __construct ($file = ', $csvDataArr = ', $specialhtml = true, $length = +, $seprator = ', ')
- {
- $this->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 < $totalRec; $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 (' & ', ' "', '" ', ' < ', ' > '), $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 (' & ', ' ' ', ' \ ', ' < ', ' > ', ' (', ') ', '% '), ', Trim ($value));
- }
- Private Function Remove_char_auto ($str, $x =0)
- {
- $x ==0? $str = $this->make_semiangle ($str): ";
- Eregi (' [[:p UNCT:] ', $STR, $arr);
- $str = Str_replace ($arr [0], ", $STR);
- return eregi (' [[:p UNCT:]] ', $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 ' and '-d ', ' e ' = ' e ',
- ' F ' = ' f ', ' g ' = ' g ', ' h ' = = ' h ', ' i ' = ' I ', ' j ' = ' j ',
- ' k ' = ' k ', ' 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 ' = ' = ', ' and ' = ', ' m ' + ' m ', ' n ' = ' n ',
- ' O ' = ' ' o ', ' p ' = ' P ', ' q ' = ' Q ', ' r ' = = ' R ', ' s ' = ' and ' s ',
- ' t ' = ' t ', ' u ' = ' = ' u ', ' v ' = ' = ' V ', ' w ' = ' W ', ' x ' = ' x ',
- ' Y ' = ' y ', ' z ' = ' z ',
- ' (' = ' = ' (') ' + ') ', ' (' = ' = ' [', ') ' + '] ', ' ' ' + ', ' [',
- ' ' ' = ' and ' ', ' ' = ' = ' [', ' ' ' ' = '] ', ' ' ' ' ' = ' [', ' ' '
- ' = ' + ' [', ' ' + '] ', ' {' = ' = ', '} ' = '} ', ' ' = ' < ',
- ' ' = ' > ',
- '% ' = '% ', ' + ' = ' + ', '-' = '-', '-' = '-', ' ~ ' = '-',
- ': ' = ': ', '. ' = = ', ', ', ' = = ', ', ', ' = ', ', ', ' = ', '
- '; ' = ', ', '? ' = = '? ', '! ' + '! ', ' ... ' + '-', ' ‖ ' = ' | ',
- ' ' ' = ' ', ' ' ' and ' ', ' ' and ' ', ' ' and ' ', ' | '
- ' = ' + ', ' $ ' + ' $ ', ' @ ' + ' @ ', ' # ' + ' # ', ' ^ ' + ' ^ ', ' & ' + ' & ', ' * ' = ' * ';
- Return Strtr ($str, $arr);
- }
- function __destruct () {
- Fclose ($this->handler);
- }
- }
- Example
- $csv = new Csv_to_array (' user.csv ');
- echo "
"; Print_r ($csv->get_array ()); echo " ";
- ?>
Copy Code
2. Use General functions
- 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 "
"; Print_r ($arr); echo " "
- ?>
Copy Code
Or
- $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 "
"; Print_r ($arrCSV); echo " ";
- ?>
Copy Code
As to which kind of better use, see their actual needs and personal hobbies, the actual work of the CSV to the array of demand is still a lot, suggest that we practice more, more master.
|