php - CSV to Associative Array -
i've seen numerous examples on how take csv file , create associative array headers keys.
for example:
brand,model,part,test honda,civic,123,244 honda,civic,135,434 toyota,supra,511,664
where create array such array[$num][$key]
$key
brand, model, part, test.
so if wanted access test value "434" have loop every index in array , ignore brands not honda, , models not civic
what need access value directly, instead of running through loop going through each $num index. want able access value test "434" with:
array['honda']['civic']['135']
or control statement looping through every model honda has... like
foreach $model in array['honda']
at least need able go through every model given known brand , access relative info each.
edit:
just confirm setting example. data has headers like:
brand model part price shipping description footnote
of need access information tied part (price, shipping,desc, footnote)
run on csv file line line, , insert array like:
$array = $fields = array(); $i = 0; $handle = @fopen("file.csv", "r"); if ($handle) { while (($row = fgetcsv($handle, 4096)) !== false) { if (empty($fields)) { $fields = $row; continue; } foreach ($row $k=>$value) { $array[$i][$fields[$k]] = $value; } $i++; } if (!feof($handle)) { echo "error: unexpected fgets() fail\n"; } fclose($handle); }
Comments
Post a Comment