Last active
August 29, 2015 14:02
-
-
Save Bolinha1/d0d58648ae09ad22713b to your computer and use it in GitHub Desktop.
Classe que transforma arquivo csv em um array associativo formando um par de chaves e valor usando o cabeçalho como chave.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Arquivo; | |
use Exception; | |
class TransformaCabecalhoCsvEmArray | |
{ | |
public function transformaCabecalhoCsvEmArray($arquivo) | |
{ | |
$linha = 1; | |
$csv = fopen($arquivo,"r"); | |
while (($dados = fgetcsv($csv, 1000, ",")) !== FALSE) | |
{ | |
$num = count ($dados); | |
for ($c=0; $c < $num; $c++) | |
{ | |
if($linha === 1) | |
$cabecalho[] = $dados[$c]; | |
} | |
$linha++; | |
} | |
return $cabecalho; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Arquivo; | |
use Exception; | |
class TransformaCsvEmArray | |
{ | |
private $arquivo; | |
private $cabecalho; | |
public function __construct($arquivo, TransformaCabecalhoCsvEmArray $cabecalho) | |
{ | |
$this->setArquivoCsv($arquivo); | |
$this->cabecalho = $cabecalho; | |
} | |
private function setArquivoCsv($arquivo) | |
{ | |
if(!file_exists($arquivo) || pathinfo($arquivo, PATHINFO_EXTENSION) !== "csv") | |
throw new Exception("Arquivo não existe, ou a extensão não é .csv"); | |
$this->arquivo = $arquivo; | |
} | |
public function transformaCsvEmArray() | |
{ | |
$array = $this->montaCabecalhoEConteudo(); | |
for($i = 0; $i < count($array); $i++) | |
{ | |
if($array[$i] !== $array[$i+1]) | |
$dadosNovos[] = $array[$i]; | |
} | |
return $dadosNovos; | |
} | |
private function montaCabecalhoEConteudo() | |
{ | |
$linha = 1; | |
$cabecalho = $this->cabecalho->transformaCabecalhoCsvEmArray($this->arquivo); | |
$totalCabecalho = count($cabecalho) - 1; | |
$csv = fopen($this->arquivo,"r"); | |
while (($dados = fgetcsv($csv, 1000, ",")) !== FALSE) | |
{ | |
$num = count ($dados); | |
if($linha > 1) | |
{ | |
for($j = 0; $j <= $totalCabecalho; $j++) | |
{ | |
$conteudo[$cabecalho[$j]] = $dados[$j]; | |
} | |
$dadosProntos[] = $conteudo; | |
} | |
$linha++; | |
} | |
return $dadosProntos; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Arquivo\TransformaCabecalhoCsvEmArray; | |
use Arquivo\TransformaCsvEmArray; | |
$arquivo = "teste.csv"; | |
$cabecalho = new TransformaCabecalhoCsvEmArray(); | |
$csv = new TransformaCsvEmArray($arquivo, $cabecalho); | |
var_dump($csv->transformaCsvEmArray()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment