Home > Backend Development > PHP Tutorial > How to read csv data and save it to an array in php, csv array_PHP tutorial

How to read csv data and save it to an array in php, csv array_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:10:24
Original
1035 people have browsed it

php method to read csv data and save it to an array, csv array

The example in this article describes how PHP reads csv data and saves it to an array. Share it with everyone for your reference. The specific analysis is as follows:

csv is a replacement for the commonly used excel format. Many times when we export data, it will be exported to csv format, which is no different from excel. The following program is to read the csv data and save it to an array. We need to operate the data. , so save to data, the code is as follows:

Copy code The code is as follows:
$info=csvtoarray::open('teste.csv');
//echo '
'; <br>
//print_r($info); <br>
//echo '
';
foreach ($info as $c)
{
echo 'Student ID:'.$c[0];
echo 'Name:'.$c[1];
echo 'Age:'.$c[2];
echo 'Height:'.$c[3].'
';
}


final class csvtoarray{

/**
* Parse the csv file into an array and return it
*
* @param string $file The csv file path to be parsed
* @param char $delimiter The content delimiter in the csv file. The default is;
* @return array
​*/
public static function open($file, $delimiter = ';'){
Return self::ordenamultiarray(self::csvarray($file, $delimiter), 1);
}

private function csvarray($file, $delimiter)
{
$result = array();
$size = filesize($file) + 1;
$file = fopen($file, 'r');
$keys = fgetcsv($file, $size, $delimiter);
fseek($file,0);//There is no original one here...Add it yourself...so that you can read the content of the first line
while ($row = fgetcsv($file, $size, $delimiter))
{
for($i = 0; $i < count($row); $i++)
{
If(array_key_exists($i, $keys))
{
$row[$keys[$i]] = $row[$i];
}  
}
Print_r($row);
$result[] = $row;
}

fclose($file);

Return $result;
}
private function ordenamultiarray($multiarray, $secondindex)
{
while (list($firstindex, ) = each($multiarray))
$indexmap[$firstindex] = $multiarray[$firstindex][$secondindex];
asort($indexmap);
while (list($firstindex, ) = each($indexmap))
if (is_numeric($firstindex))
$sortedarray[] = $multiarray[$firstindex];
else $sortedarray[$firstindex] = $multiarray[$firstindex];
Return $sortedarray;
}
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/936798.htmlTechArticleHow to read csv data in php and save it to an array, csv array This article describes the example of how php reads csv data and saves it to Array methods. Share it with everyone for your reference. The specific analysis is as follows: csv is...
Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template