Badly formatted CSV String to Associative Array

At present my job is filled with API calls and connections. Some APIs have amazing documentation, all the functions you could ever want and great verbose data return in an expected and properly formatted response. Other APIs not so much. My current predicament is that the system I’m calling to is responding with what looks like a csv but it is literally just comma separated values over several lines in a string. So quotation marks freak out normal csv to array methods, and the line breaks mess up a straight explode method. *Inesrt heavy sigh here*

Even after overcoming those issues your left with an indexed array, where the first index is the names of your values and not an associative array. So here’s what I came up with while messing around trying to bend this APIs stupid csv string response to my will.

function csvToArray($result){
//break up the lines into an array
$csv = explode("\n",$result);

//create our array to hold the data
$out = array();
$tmpOut = array();

//create indexed array
foreach($csv as $ln){
$tmp = str_getcsv($ln);
array_push($tmpOut, $tmp);
}

//create assoc array using the the first index as keys
for($i = 0; $i < count($tmpOut); $i++){
$tmp = array_combine($tmpOut[0],$tmpOut[$i]);
array_push($out, $tmp);
}

//remove redundant first index as it is just the [keys] => keys
$keys = array_shift($out);

//return our associative array
return $out;
}

Let the world know