I wanted the best of the 2 solutions by james at moss dot io and Jay Williams (csv_to_array()) - create associative array from a CSV file with a header row.
<?php
$array = array_map('str_getcsv', file('data.csv'));
$header = array_shift($array);
array_walk($array, '_combine_array', $header);
function _combine_array(&$row, $key, $header) {
$row = array_combine($header, $row);
}
?>
Then I thought why not try some benchmarking? I grabbed a sample CSV file with 50,000 rows (10 columns each) and Vulcan Logic Disassembler (VLD) which hooks into the Zend Engine and dumps all the opcodes (execution units) of a script - see http://pecl.php.net/package/vld and example here: http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster
Result:
array_walk() and array_map() - 39 opcodes
csv_to_array() - 69 opcodes