Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:将PHP字典数据集转换为key:valu对_Php_Json_Codeigniter_Key Value - Fatal编程技术网

PHP:将PHP字典数据集转换为key:valu对

PHP:将PHP字典数据集转换为key:valu对,php,json,codeigniter,key-value,Php,Json,Codeigniter,Key Value,我将数据集smiller设置为DICTORIAL,我想获得该数据的键值对。 以下是数据集: $data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}" 我正在做json_解码($data,true)但是没有运气 对不起,我不清楚 顺便说一句,我是用PHP做的 结果应该是这样的: test_field2: NONE test_f

我将数据集smiller设置为DICTORIAL,我想获得该数据的键值对。 以下是数据集:

$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
我正在做
json_解码($data,true)但是没有运气

对不起,我不清楚

顺便说一句,我是用PHP做的

结果应该是这样的:

test_field2: NONE
test_field3: NONE

由于数据中的
u
,因此您的数据是无效的json,因此这里有一个解决方案

 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>';
    ?>
    
    
    

    演示:

    无效JSON…我担心这不起作用,因为该字符串无效JSON您从哪里获取该字符串?我从数据库获取此数据。还有一个基于django框架的整个后端过程,它操作数据并将数据保存到数据库中。django代码似乎被破坏了。我会先看看这个问题,而不是用PHP来解决它。这是治疗症状而不是杀死感染。我测试了它,但仍然给出空值,因为JSON需要双引号,而不是单引号更新以反映单引号到双引号可能,我只提供上面列出的数据的答案,在100%的情况下,这不是一个完整的解决方案。这个数据中包含一个
    u
    的事实是非常可疑的,我怀疑将来任何类型的
    str\u replace
    都会被破坏,这可能会使JSON有效,但是去掉所有的u是一个坏主意idea@Devon是的,你是ri8,这是我的错。请参见“立即编辑:”),如果任何值以
    u
    结尾,会发生什么情况?