我需要将字符串参数解析为数组
例子:
p10s2z1234
输出
Array(
'p' => 10,
's' => 2,
'z' => 1234
)
感谢任何提前。
使用正则表达式获取所需的值,然后组合数组以获得关联数组。例如:
$str = 'p10s2z1234';
preg_match_all('/([a-z]+)(\d+)/', $str, $matches); //handles only lower case chars. feel free to extend regex
print_r(array_combine($matches[1], $matches[2]));
我会说你应该把它分开 p,10,s,2,z,1234 和 explode()
$test = "p,10,s,2,z,1234"; $pieces = explode(",", $test);
然后将它们放入一个数组中。