Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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-重命名JSON数组中的重复键_Php_Arrays_Json - Fatal编程技术网

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

PHP-重命名JSON数组中的重复键,php,arrays,json,Php,Arrays,Json,更多信息: 我忘了提到像列表这样的项目。。。是随机生成的 我有一个文本,我使用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); 但问题是,当我将其转换为array

更多信息: 我忘了提到像列表这样的项目。。。是随机生成的

我有一个文本,我使用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);
但问题是,当我将其转换为array
echo$tree时返回我

Array
(
    [list_Gentlemen] => root
    [list_Gold] => list_Ladies
    [list_Ladies] => root
    [list_Plata] => list_Ladies
)
我的意思是有一个重复键
[list\u Gold]
,它不会插入重复键。有没有办法重命名那把钥匙

Array
(
    [list_Gentlemen] => root
    [list_Gold] => list_Gentlemen
    [list_Ladies] => root
    [list_Plata] => list_Ladies
    [list_Gold] => list_Ladies
)
感谢您的帮助。

更新: 您可以用一些正则表达式替换重复的键,但这仅在每个键最多有2个重复项时有效:
$tree=preg\u replace('/\[(\w{2,})(?=.*?\\1)\]\w*/','[$1\u 2]=',$tree)
这将有以下输出:
list[gunds_2]=null&list[Gold_2]=gunds&list[lades_2]=null&list[Plata]=lades&list[Gold]=lades

使用重复键(
list\u Gold
)的数组是不可能的,因为PHP数组中不支持重复键。您可以做的是,在解码之前解析JSON字符串,并重命名重复项(例如,如果只有这一个索引,您可以始终使用
列表黄金2
替换第二个匹配的
列表黄金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个重复项时有效:
$tree=preg\u replace('/\[(\w{2,})(?=.*?\\1)\]\w*/','[$1\u 2]=',$tree)
这将有以下输出:
list[gunds_2]=null&list[Gold_2]=gunds&list[lades_2]=null&list[Plata]=lades&list[Gold]=lades

使用重复键(
list\u Gold
)的数组是不可能的,因为PHP数组中不支持重复键。您可以做的是,在解码之前解析JSON字符串,并重命名重复项(例如,如果只有这一个索引,您可以始终使用
列表黄金2
替换第二个匹配的
列表黄金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
)

谢谢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]';
但我不知道如何删除括号周围的文本,然后就可以很容易地使用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);

谢谢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]';
但我不知道如何删除括号周围的文本,然后就可以很容易地使用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);

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

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

preg_match_all('~(,|\{)\s*"([^"]*)"\s*:~', $tree, $matches, PREG_SET_ORDER);
foreach ($matches as $i => $m) {
    $tree = implode($m[1].'"'.$i.'-'.$m[2].'":', explode($m[0], $tree, 2));
}

print_r(json_decode($tree, 1));
或创建多维数组:

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

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

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

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

preg_match_all('~(,|\{)\s*"([^"]*)"\s*:~', $tree, $matches, PREG_SET_ORDER);
foreach ($matches as $i => $m) {
    $tree = implode($m[1].'"'.$i.'-'.$m[2].'":', explode($m[0], $tree, 2));
}

print_r(json_decode($tree, 1));
或创建多维数组:

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

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

检查这里:检查这里:您好,谢谢您的回答,实际上它工作得很好,但是项目是随机生成的“list”\有可能修复json来源处的重复条目吗?问题是json实际上也不支持重复的键,所以基本上你的json字符串是无效的…我之前有这样的$tree='list[guests]=null&list[Gold]=guests&list[ladys]=null&list[Plata]=ladys&list[Gold]=ladys';您的意思是,每个键都有多个数组,格式为
list[key]=value
?这些数据是否来自GET或POST请求,或者字符串的格式是什么?如果将所有这些数组放在一个数组中(或$\u GET或类似的东西),您可以更好地解析它们,因为您没有重复的键,遍历这些数组并将其合并到一个数组中,在该数组中重命名重复的键。下面是我所拥有的Hi Dion,感谢您的回答,实际上它非常有效,但是这些条目是以“列表”的形式生成的。有可能修复json来源处的重复条目吗?问题是json实际上也不支持重复的键,所以基本上你的json字符串是无效的…我之前有这样的$tree='list[guests]=null&list[Gold]=guests&list[ladys]=null&list[Plata]=ladys&list[Gold]=ladys';您的意思是,每个键都有多个数组,格式为
list[key]=value
?这些数据是否来自GET或POST请求,或者字符串的格式是什么?如果您将所有这些数组都放在一个数组中(或$\u GET或类似的东西),您可以更好地解析它们,因为您没有重复的键,遍历这些数组并将其合并到一个数组中,在该数组中重命名重复的键。下面是我所拥有的,您可以添加如何将其转换为json的代码吗?您需要从字符串中获得哪些确切数据?因为在上一个示例中(使用
[guester][Gold]…
),您不再拥有这些值。$tree='list[guests]=null&list[Gold]=guests&list[ladys]=null&list[Plata]=ladys&list[Gold]=ladys'$tree=str_replace('=null','':“root',$tree)$tree=str_replace(“=”,“:”列表“,$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);我在上面更新了你的帖子,并在那里添加了代码以提高可读性,但我现在得到了你想要实现的目标。你只有一个