PHP string to array

2020-04-20 06:47发布

I have a string that when I var_dump returns the following

string(20) "{\"key1\":\"key1_value",\"key2\":\"key2_value\"}"

How can I convert that into an array that will return the following when I var_dump?

array(2) { ["key1"]=>  string(20) "key1_value" ["key2"]=>  string(20) "key2_value" } 

Thanks,
Tee

标签: php
4条回答
beautiful°
2楼-- · 2020-04-20 07:20

What you have as data looks like valid JSON. You probably can use json_decode with the second parameter to true (to get an associative array) like this:

$array = json_decode($string, true);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-04-20 07:29

It looks like a simple JSON array that got mangled by PHP's magic_quotes or some other escaping function. Turn off magic_quotes and run json_decode() on the string.

// If you cannot disable `magic_quotes` or you escaped it manually, use this
$array = json_decode(stripslashes($strings), true);
查看更多
女痞
4楼-- · 2020-04-20 07:39

The explode function will get you what you need.

查看更多
▲ chillily
5楼-- · 2020-04-20 07:41
explode(',\\',$string); 

should do the trick.

查看更多
登录 后发表回答