Skip to content

Instantly share code, notes, and snippets.

@aleckyann
Created May 13, 2018 23:23
Show Gist options
  • Save aleckyann/5fccca2dba3a63fdfada6ac73f58c66b to your computer and use it in GitHub Desktop.
Save aleckyann/5fccca2dba3a63fdfada6ac73f58c66b to your computer and use it in GitHub Desktop.
Funções para ler e escrever arquivos .csv
<?php
/**
* exemplo: $array_de_arrays = array(
* array('linha1.coluna1', 'linha1.coluna2', 'linha1.coluna3'),
* array('llinha2.coluna1', 'linha2.coluna2', 'linha2.coluna3')
* );
**/
function writeCSV($local_do_arquivo, $array_de_arrays) {
$arquivo = fopen($local_do_arquivo, 'w');
foreach ($array_de_arrays as $array) {
fputcsv($fp, $array);
}
fclose($arquivo);
}
/**
* exemplo arquivo csv: a,b,c,d,e,f,g
* h,i,j,k,l,m,n
* o,p,q,r,s,t,u
**/
function readCSV($local_do_arquivo) {
$arquivo = fopen($local_do_arquivo, "r");
$coluna = 0;
while ($line = fgetcsv($arquivo, 1000, ",")) {
if ($coluna++ == 0) {
continue;
}
$equipamentos[] = [
'linhaX.coluna1' => $line[0],
'linhaX.coluna2' => $line[1],
'linhaX.coluna3' => $line[2],
'linhaX.coluna4' => $line[3],
'linhaX.coluna5' => $line[4],
'linhaX.coluna6' => $line[5],
'linhaX.coluna7' => $line[6],
'linhaX.coluna8' => $line[7]
];
}
fclose($arquivo);
return $equipamentos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment