从包含多个键的字符串中提取键

I'm searching for a way to easly extract a string containing multiple keys. The string is a result form a curl header response

echo $response['body'];
// status=2 reason=Invalid tariff/currency

Desired result:

$status == '2';
$reason == 'Invalid tariff/currency';

or

array (
    [status] => '2'
    [reason] => 'Invalid tariff/currency'
)

Something like this, perhaps?

$parts = explode(" ", $response['body'], 2);
foreach($parts as $part)
{
   $tmp = explode("=", $part);
   $data[$tmp[0]] = $tmp[1];
}

var_dump($data);

Given the string above, you can create local variables $status and $reason using PHP Variable variables. Take a look at this code:

$str = 'status=2 reason=Invalid tariff/currency';

foreach (explode(' ', $str, 2) as $item) {
    list($key, $val) = explode('=', $item);
    $$key = $val;
}

// Now you have these
echo $status;  // 2
echo $reason;  // Invalid tariff/currency

try this, it will work only for your example, I suggest better to go with preg_match if you have possibility of extracting data from different formats.

$response['body'] = "status=2 reason=Invalid tariff/currency";
$responseArray = explode(" ", $response['body'], 2);
foreach($responseArray as $key => $value){
    $requiredOutput = explode("=",$value);
    print_r($requiredOutput);
}