PHP array with value returns NULL -
i have array converted csv file. have there 2 keys id , name.
if show key name ok. when tried key id, null key id have value set.
function convertcsvtoarray($filename='', $delimiter=';') { if(!file_exists($filename)) return false; $header = null; $data = array(); if (($handle = fopen($filename, 'r')) !== false) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { if(!$header) $header = $row; else $data[] = array_combine($header, $row); } fclose($handle); } return $data; } $array = convertcsvtoarray('/path/to/csv/categories.csv',';'); /* $array structure array(2){ ["id"] => 3 ["name"] => name } */ foreach($array $category){ var_dump($category["id"]); //return null var_dump($category["name"]); //return "some name" }
csv dump
id;name 3;značkové nealko nápoje 4;nízkoenergetické nápoje 5;minerálne vody, sóda 6;tetrapack 0.2l 0.5l
print_r $array
array ( [0] => array ( [id] => 3 [name] => značkové nealko nápoje ) [1] => array ( [id] => 4 [name] => nízkoenergetické nápoje ) )
print_r $category
array ( [id] => 3 [name] => značkové nealko nápoje )
the problem here comes bom.
invisible characters added @ begining of file, prefixed "id" key, php can't find id key, , shows null values.
convert csv file utf-8 without bom , fix problem.
Comments
Post a Comment