I want to get values ​​from columns by column header. I manage to get any, except the first. Why is that? Gives an error message

Notice: Undefined index: ZIP

$file_name="http://www.omniva.ee/locations.csv"; $fp = fopen($file_name, 'r'); $head = fgetcsv($fp, 4096, ';', '"'); while($column = fgetcsv($fp, 4096, ';', '"')) { $column = array_combine($head, $column); echo $column['ZIP']; // не получаю echo $column['NAME']; // получаю } 
  • one
    Add an example of an entry from a csv file, perhaps an error in it, and not in the code - ALexander
  • one
    link in the code to the file working. Part of the file added here if it is more convenient to pastebin.com/fWdw2Xvd - akasergej

2 answers 2

working version

 $file_name="http://www.omniva.ee/locations.csv"; $fp = fopen($file_name, 'r'); $head = fgetcsv($fp, 4096, ';', '"'); $head[0] = 'ZIP'; # это фикс! while($column = fgetcsv($fp, 4096, ';', '"')) { $column = array_combine($head, $column); echo $column['ZIP']; // не получаю echo $column['NAME']; // получаю } ?> 
  • you can still save files with BOM -utf8 - L. Vadim

It's simple. At the very beginning of the file there are two "left bytes". this is the so-called bom . There are many ways to remove them, but I think that in your case the simplest solution will do.

 $file_name="http://www.omniva.ee/locations.csv"; $fp = fopen($file_name, 'r'); $head = fgetcsv($fp, 4096, ';', '"'); $head[0] = 'ZIP'; # это фикс! while($column = fgetcsv($fp, 4096, ';', '"')) { $column = array_combine($head, $column); echo $column['ZIP']; // не получаю echo $column['NAME']; // получаю } 

BTW, there is a zip field, it only looks like <U+FEFF>ZIP .

  • your fix does not work for me - L. Vadim
  • Insert print_r($column); to the place of fixation and everything will become obvious. - KoVadim
  • Oh, not column, but head - KoVadim