PHP : 将php字典数据集转换为key:valu对
PHP : Convert php dictionary data set to key:valu pair
我有数据集 smiller 到字典,我想获取此数据的键值对。
这是数据集:
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
我正在做 json_decode($data, true);
但运气不好
对不起如果我是unclear.f
顺便说一句,我正在 PHP
结果应该是这样的:
test_field2: NONE
test_field3: NONE
因为你的数据无效json因为u
这里有一个解决方案
json_decode(str_replace("'",'"',str_replace("u'","'",$data)), true);
应该可以解决问题
您必须这样尝试,因为您现有的代码存在几个问题。
- 将不必要的字符
u'
替换为 '
- 替换 json 字符串上的单引号 (
'
)。对于 json 双引号 ("
) 是允许的。
所以搜索这两个字符,分别替换为''
和"
<?php
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}";
$search = ["u'","'"];
$replace = ["'",'"'];
$without_u = str_replace($search,$replace,$data);
$array = json_decode($without_u, true);
print '<pre>';
print_r($array);
print '</pre>';
?>
我有数据集 smiller 到字典,我想获取此数据的键值对。 这是数据集:
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
我正在做 json_decode($data, true);
但运气不好
对不起如果我是unclear.f
顺便说一句,我正在 PHP
结果应该是这样的:
test_field2: NONE
test_field3: NONE
因为你的数据无效json因为u
这里有一个解决方案
json_decode(str_replace("'",'"',str_replace("u'","'",$data)), true);
应该可以解决问题
您必须这样尝试,因为您现有的代码存在几个问题。
- 将不必要的字符
u'
替换为'
- 替换 json 字符串上的单引号 (
'
)。对于 json 双引号 ("
) 是允许的。
所以搜索这两个字符,分别替换为''
和"
<?php
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}";
$search = ["u'","'"];
$replace = ["'",'"'];
$without_u = str_replace($search,$replace,$data);
$array = json_decode($without_u, true);
print '<pre>';
print_r($array);
print '</pre>';
?>