PHP - 重命名JSON数组中的重复键

时间:2016-11-08 18:48:37

标签: php arrays json

更多信息: 我忘了提到像list _...这样的项目是随机生成的

我有一个文本,我使用json

将其转换为数组
$tree = '{"list_Gentlemen":"root","list_Gold":"list_Gentlemen","list_Ladies":"root","list_Plata":"list_Ladies","list_Gold":"list_Ladies"}';

我用

转换它
$tree = json_decode($tree,true);

但问题是,当我将其转换为数组echo $tree;时,请返回

Array
(
    [list_Gentlemen] => root
    [list_Gold] => list_Ladies
    [list_Ladies] => root
    [list_Plata] => list_Ladies
)

我的意思是有一个重复键[list_Gold],它不会插入重复键。有没有办法重命名该密钥?

Array
(
    [list_Gentlemen] => root
    [list_Gold] => list_Gentlemen
    [list_Ladies] => root
    [list_Plata] => list_Ladies
    [list_Gold] => list_Ladies
)

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

你可以添加数组项的索引,因此不能有任何双打:

Array
(
    [0-list_Gentlemen] => root
    [1-list_Gold] => list_Gentlemen
    [2-list_Ladies] => root
    [3-list_Plata] => list_Ladies
    [4-list_Gold] => list_Ladies
)

结果:

<?php
$tree = '{"list_Gentlemen":"root","list_Gold":"list_Gentlemen","list_Ladies":"root","list_Plata":"list_Ladies","list_Gold":"list_Ladies"}';

$tree = preg_replace('~(,|\{)(\s*"[^"]*"\s*:\s*"[^"]*")~', '$1{$2}', trim($tree));
$tree = '['.substr($tree, 1, strlen($tree) - 2).']';

print_r(json_decode($tree, 1));

或创建一个多维数组:

Array
(
    [0] => Array
        (
            [list_Gentlemen] => root
        )

    [1] => Array
        (
            [list_Gold] => list_Gentlemen
        )

    [2] => Array
        (
            [list_Ladies] => root
        )

    [3] => Array
        (
            [list_Plata] => list_Ladies
        )

    [4] => Array
        (
            [list_Gold] => list_Ladies
        )

)

结果:

[{"key":"value"},{"key","value"}]

编辑:如果您可以控制json的样式,您可以通过以下方式生成它:{{1}},这样您就可以跳过我的第二个解决方案的正则表达式部分

答案 1 :(得分:1)

更新: 您可以使用某些正则表达式替换重复的键,但这仅在每个键最多重复2次时才有效: $tree = preg_replace('/\[(\w{2,})(?=.*?\\1)\]\W*/', '[$1_2]=', $tree); 这将有以下输出:list[Gentlemen_2]=null&list[Gold_2]=Gentlemen&list[Ladies_2]=null&list[Plata]=Ladies&list[Gold]=Ladies

不能使用具有重复键(list_Gold)的数组,因为不支持PHP数组中的副本。您可以做的是在解码之前解析JSON字符串,并重命名重复项(如果只有这一个索引,您可以始终将list_Gold的第二个匹配替换为list_Gold_2)。

这可能是这样的:

$tree1 = substr($tree, 0 , strpos($tree, 'list_Gold') + 2);
$tree2 = substr($tree, strpos($tree,'list_Gold') + 2);
$tree2 = str_replace('list_Gold', 'list_Gold_2', $tree2);
$tree = $tree1 . $tree2;
$treeArray = json_decode($tree, true);

上面数组的内容:

Array
(
    [list_Gentlemen] => root
    [list_Gold] => list_Gentlemen
    [list_Ladies] => root
    [list_Plata] => list_Ladies
    [list_Gold_2] => list_Ladies
)

答案 2 :(得分:0)

感谢Dion,唯一的问题是我只能访问:

$tree = 'list[Gentlemen]=null&list[Gold]=Gentlemen&list[Ladies]=null&list[Silver]=Ladies&list[Gold]=Ladies'; 

我做了转换为json数组格式,我正在考虑使用像这样的str_replace

$text = '[Gentlemen] [Gold] [Ladies] [Silver] [Gold]';

但我不知道如何删除文本arround括号,之后使用explode或类似的东西很容易修改它们

转换为JSON的代码:

$tree = 'list[Gentlemen]=null&list[Gold]=Gentlemen&list[Ladies]=null‌​&list[Plata]=Ladies&‌​list[Gold]=Ladies';
$tree = str_replace('=null','":"root',$tree);
$tree = str_replace('=','":"list_',$tree);
$tree = str_replace('[','_',$tree);
$tree = str_replace(']','',$tree);
$tree = str_replace('&','","',$tree);
$tree = '{"'.$tree.'"}';
$tree = str_replace('=null','":"root',$tree); $tree = json_decode($tree,true);