doswy02440 2018-01-19 17:10
浏览 31
已采纳

如何将包含关联数组的字符串转换为关联数组类型?

I have a string that contains associative array like that

$string = "['key1'=>'value1','key2'=>'value2','key3'=>'value3']";

and I want to convert it to an associative array so i can loop through it.

is there any way to do it ?

Thanks in advance .

  • 写回答

2条回答 默认 最新

  • dougaopu7938 2018-01-19 17:19
    关注

    Very simple way is convert that string to json and decode

    $string = str_replace(["'", '=>', '[', ']'], ['"', ':', '{', '}'], $string);
    print_r(json_decode($string, true));
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?