0

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

2 Answers 2

1

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

$header = fgetcsv($file, 1000, ';');
Sign up to request clarification or add additional context in comments.

1 Comment

I've done this, but the same thinks 0 => b"Código do Imóvel" 1 => "FotoImovel" 2 => "destaque" 3 => "ordem"
1

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.

5 Comments

As they are reading the header separated by , (the default) the header shown in the question shows it is all one field. So the data is unlikely to match the header in the array_combine()
@NigelRen I see OP's code already use ; as separator but maybe he forgot to update the "output" section. If both header and rows use same separator, all lines will be read as one field and array_combine would not fail
$header = fgetcsv($file);?
the problem is that "b" appears in some lines : 0 => b"Código do Imóvel;FotoImovel;destaque;ordem"
I've tried fgetcsv($file) and fgetcsv($file, 1000, ';'); too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.