I needed to read CSV files into associative arrays with column headers as keys. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr() below allowed me to pad either array or not when parsing my CSVs to arrays.
$a is the array of header columns and $b is an array of the current row retrieved with fgetcsv()
<?php
function array_combine_special($a, $b, $pad = TRUE) {
$acount = count($a);
$bcount = count($b);
if (!$pad) {
$size = ($acount > $bcount) ? $bcount : $acount;
$a = array_slice($a, 0, $size);
$b = array_slice($b, 0, $size);
} else {
if ($acount > $bcount) {
$more = $acount - $bcount;
$more = $acount - $bcount;
for($i = 0; $i < $more; $i++) {
$b[] = "";
}
} else if ($acount < $bcount) {
$more = $bcount - $acount;
for($i = 0; $i < $more; $i++) {
$key = 'extra_field_0' . $i;
$a[] = $key;
}
}
}
return array_combine($a, $b);
}
?>