PHP: How to quickly split a key=value file into associative array



PHP Snippet 1:

foreach($response_lines as $line) {
    $bits = explode('=', $line, 2);
    $fields[$bits[0]] = $bits[1];
}

PHP Snippet 2:

foreach($lines as $line) {
    $lineData = preg-split("\s*=\s*",$line);
    if(count($lineData)==2) {
         $result[$lineData[0]] = $lineData[1];
    }
}

PHP Snippet 3:

foreach($response as $line) {
    if (strpos($line, "=") === FALSE) { continue; }   // Skip the line if there's no assignment action going on.
    list($var, $value) = explode("=", trim($line));   // Get the parts on either side of the "=".
    $result[$var] = (empty($value) ? NULL : $value);  // Assign the value to the result array, making the value NULL if it's empty.
}

PHP Snippet 4:

$lines = explode("\n", $paypal_response);
$lines = array_map('trim', $lines);
$status = array_shift($lines);
if ($status != 'SUCCESS') {
    // Transaction was not successful
}
$values = array();
foreach($lines as $line) {
    list($key, $value) = explode('=', $line, 2);
    $values[$key] = $value;
}

PHP Snippet 5:

//$result=the result from paypal
parse_str(str_replace(PHP_EOL,'&',$result),$result);
var_dump($result);