PHP - 按键/值对拆分字符串
我有一个这样的字符串:
键=值,键 2=值2
我想把它解析成这样的东西:
array(
"key" => "value",
"key2" => "value2"
)
我可以做这样的事情
$parts = explode(",", $string)
$parts = array_map("trim", $parts);
foreach($parts as $currentPart)
{
list($key, $value) = explode("=", $currentPart);
$keyValues[$key] = $value;
}
但这似乎很荒谬。一定有办法用PHP更聪明地做到这一点吧?