Reputation: 2262
I have a csv table, with the first row as the header. I would like to iterate through all the rows, using the name of the column to refer to the column instead of its numerical value. To do this, I think as I iterate I need to convert each numerically indexed row into an associative one, but I can't figure out the best way to do this.
$headerrow = str_getcsv($table[0]); //gives me an array like 0=>foo,1=>bar,2=>bat
foreach ($table as $rownumber=>$row){
if($rownumber!=0){
$rowarray=str_getcsv($row);//gives me an array like 0=>blah,1=>blah,2=>blah
//how do I get $rowarray['foo'] or $rowarray['bar'] most efficiently?
}
}
Upvotes: 1
Views: 664
Reputation: 2262
Guess I should have looked more closely at the array functions. I found array_combine.
$rowarray = array_combine($headerrow,str_getcsv($row));
print($rowarray['foo']);
Upvotes: 1