PHP read CSV data saved to the array code CSV is a common alternative to Excel format Oh, many times we export data will be in CSV format, so and Excel no difference, the following program is to read the CSV data saved to the array we want to manipulate the data, so save to the data.
PHP Tutorial read CSV data saved to array code
CSV is a common alternative to Excel format Oh, many times we export data will be in CSV format, so and Excel no difference, the following program is to read the CSV data saved to the array we want to manipulate the data, so save to the data.
$info =csvtoarray::open (' teste.csv ');
Echo '
';
Print_r ($info);
Echo '
';
foreach ($info as $c)
{
Echo ' Study 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 to return
*
* @param string $file The path to the CSV file to parse
* @param char $delimiter the contents of the CSV file are separated defaults think;
* @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);//Here the original is not. Add yourself. This will read the contents 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;
}
}
http://www.bkjia.com/PHPjc/444895.html www.bkjia.com true http://www.bkjia.com/PHPjc/444895.html techarticle PHP read CSV data saved to the array code CSV is a common alternative to Excel format Oh, many times we export data will be in CSV format, so and Excel no difference, the following ...