1

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?
    }
}

1 Answer 1

1

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']);
Sign up to request clarification or add additional context in comments.

1 Comment

Exactomundo. $combined = array_combine($keys, $values)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.