First key of php associative array returns undefined index when parsed from CSV -


i having trouble accessing first index of associative array when parsing csv.

csv:

id,color 1,red 2,green  3,blue 

php:

function associative_array_from_csv($csv) {     $rows   = array_map('str_getcsv', file($csv));     $header = array_shift($rows);     $array  = array();      foreach($rows $data) {         $array[] = array_combine($header, $data);     }      return $array; }  $colors = associative_array_from_csv($csv); 

now $colors returns:

[     [         "id"    => "1",         "color" => "red",     ],     [         "id"    => "2",         "color" => "green ",     ],     [         "id"    => "3",         "color" => "blue",     ], ]; 

but if try access id of color:

$colors[0]["id"]    // returns undefined index: id $colors[0]["color"] // returns "red" 

if loop on colors can access id so:

foreach($colors $color)  {      print_r(reset($color)); // prints id  } 

but why can't access directly $colors[0]["id"]?

thanks


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -