php - 如何将下面的字符串转成数据 键值对应 不使用 eval
巴扎黑
巴扎黑 2017-04-11 10:03:24
0
3
550
$str = 'array("1"=>"给对方","2"=>"发鬼地方","5"=>"","6"=>"发鬼地方","7"=>"发光飞碟");';
巴扎黑
巴扎黑

reply all(3)
迷茫

参考@oraoto

$str = 'array("1"=>"给对方","2"=>"发鬼地方","5"=>"","6"=>"发鬼地方","7"=>"发光飞碟");';
$s= str_replace(['array(','=>',');'],['{',':','}'],$str);
print_r(json_decode($s,1));
Array
(
    [1] => 给对方
    [2] => 发鬼地方
    [5] => 
    [6] => 发鬼地方
    [7] => 发光飞碟
)
伊谢尔伦

思路:

  1. 去掉开头的array(和最后的);,变成:

    '"1"=>"给对方","2"=>"发鬼地方","5"=>"","6"=>"发鬼地方","7"=>"发光飞碟"'
  2. =>替换成:,变成:

    '"1":"给对方","2":"发鬼地方","5":"","6":"发鬼地方","7":"发光飞碟"'
  3. 前后加{},变成:

    '{"1":"给对方","2":"发鬼地方","5":"","6":"发鬼地方","7":"发光飞碟"}'
  4. json_decode

Peter_Zhu

用正则处理,前提是你的value里面不会有 => 这样的文字

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!