将此字符串转换为此数组的最快方法是什么?
$string = 'a="b" c="d" e="f"';
Array (
a => b
c => d
e => f
)
Run Code Online (Sandbox Code Playgroud)
假设它们总是用空格分隔,并且值总是被引号括起来,你可以explode()两次并去除引号.可能有更快的方法来做到这一点,但这种方法非常简单.
$string = 'a="b" c="d" e="f"';
// Output array
$ouput = array();
// Split the string on spaces...
$temp = explode(" ", $string);
// Iterate over each key="val" group
foreach ($temp as $t) {
// Split it on the =
$pair = explode("=", $t);
// Append to the output array using the first component as key
// and the second component (without quotes) as the value
$output[$pair[0]] = str_replace('"', '', $pair[1]);
}
print_r($output);
array(3) {
["a"]=>
string(1) "b"
["c"]=>
string(1) "d"
["e"]=>
string(1) "f"
}
Run Code Online (Sandbox Code Playgroud)