Rafael Moura
Rafael Moura

Reputation: 1327

Remove characters in array from file CSV PHP

Hey guys I'm geting a file CSV and when use print I have the output

 $excelImoveis = $request->file('excel_imoveis');
 $filePath = $excelImoveis->getRealPath();
 $file = fopen($filePath, 'r');
 $header = fgetcsv($file);

 $array = [];   

 dd($header);

 while (($columns = fgetcsv($file, 1000, ';')) !== FALSE) { 
      dd($header);
        $array[] = array_combine(array_filter($header), array_filter($columns));
 }

Outupu

array:1 [▼
  0 => b"Código do Imóvel;FotoImovel;destaque;ordem"
]

How to remove "b" this item ? maybe this implied the error of array_conbine():

$array[] = array_combine(array_filter($header), array_filter($columns));

Error: array_combine(): Both parameters should have an equal number of elements

Upvotes: 0

Views: 154

Answers (2)

Anggara
Anggara

Reputation: 637

array_filter removes empty-evaluated values (such as 0, "0", "", null, false). So if one of your column has empty value, the number of elements will be different with header's and array_combine will fail.

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94642

Change the line that reads the file header to use the correct seperator

$header = fgetcsv($file, 1000, ';');

Upvotes: 1

Related Questions