时间:2021-07-01 10:21:17 帮助过:15人阅读
$str = "aa:1,bb:2,cc:3,dd:4"; parse_str(preg_replace(array('/\w+/', '/:/','/,/'), array('"$0"','=','&'), $str), $ar); print_r($ar);
------解决方案--------------------
再来二种
$str = "aa:1,bb:2,cc:3,dd:4"; foreach(array_chunk(preg_split('/[:,]/', $str), 2) as $t) $ar[$t[0]] = $t[1]; print_r($ar);
------解决方案--------------------
添块砖
$str = "aa:1,bb:2,cc:3,dd:4"; preg_replace("#([\w]+):([\d]+)#e", "\$arr['\\1']=\\2\\2", $str); print_r($arr); /* Array ( [aa] => 11 [bb] => 22 [cc] => 33 [dd] => 44 ) */
------解决方案--------------------
[User:root Time:14:14:44 Path:/home/liangdong/php]$ php arr.php 1 2 3 4 [User:root Time:14:14:45 Path:/home/liangdong/php]$ cat arr.php