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

How to read csv data and save it to an array in php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:59:24
Original
929 people have browsed it

How to read csv data in php and save it into an array

This article mainly introduces how to read csv data in php and save it into an array. This is achieved through encapsulated class files. Function is a practical skill for operating csv files. Friends who need it can refer to it

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:

The code is as follows:

$info=csvtoarray::open('teste.csv');
//echo '
'; <br>
//print_r($info); <br>
//echo '
';
foreach ($info as $c)
{
echo 'Student number:'.$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/975891.htmlTechArticleHow to read csv data in php and save it to an array. This article mainly introduces how to read csv data in php and save it to an array. The method realizes this function through encapsulated class files, which is the implementation of csv file operation...
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