php用explode方法将两组字符串按要求打散为数组
2020-07-03 网络
两组字符串为:
$str = '111,aaa|222,bbb|333,ccc'; $con = ['id', 'name'];
PHP实现源码:
$str = '111,aaa|222,bbb|333,ccc'; $con = ['id', 'name']; foreach(explode('|', $str) as $v) { $res[] = array_combine($con, explode(',', $v)); } var_dump($res);
输出结果如下:
array(3) { [0]=> array(2) { ["id"]=> string(3) "111" ["name"]=> string(3) "aaa" } [1]=> array(2) { ["id"]=> string(3) "222" ["name"]=> string(3) "bbb" } [2]=> array(2) { ["id"]=> string(3) "333" ["name"]=> string(3) "ccc" } }