PHP在JSON阵列
更多信息重命名重复键: 我忘了,更不用说像列表项_...是我一直在使用JSONPHP在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;
return me
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
)
感谢您的帮助。
更新: 您可以取代一些正则表达式的重复键,但是这只是工作,如果有最多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
有一个数组重复密钥()是不可能的,因为PHP数组中的重复项不受支持。你可以做的是在解码之前解析JSON字符串,并重命名重复项(如果只有这一个索引,你可以总是用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
)
嗨Dion,谢谢你的回答,实际上它很好用,但是项目生成ramdomly“list _...” – user2312198
可以吗是否有可能修复重复的条目在JSON来自哪里?问题是,json实际上不支持重复键,所以基本上你的json字符串是无效的... – Dion
我有这样的之前,$ tree ='list [Gentlemen] = null&list [Gold] = Gentlemen&list [Ladies] = NULL&列表[Plata的] =女子列表[金] =女士; – user2312198
感谢翁,唯一的事情是,我只有存取权限:
$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]';
但我不知道如何删除文本以防万一支架,然后后它会很容易他们使用爆炸或类似的转换是
代码以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);
你可以添加如何将其转换为json的代码?你需要什么确切的数据从字符串?因为在最后一个例子中(用[[Gentleman] [Gold] ...'),你不再有价值。 – Dion
$ tree ='list [Gentlemen] = null&list [Gold] =绅士&列表[女士] =空&列表[Plata] =女士&列表[Gold] =女士'; $ 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); – user2312198
我更新了上面的文章,并在其中添加了代码为了更好的可读性,但是我现在已经得到了你想要达到的效果,你只需要括号内的数值吧?顺便说一下,你应该编辑你的实际问题帖子,而不是用更多细节添加一个答案给你自己的问题;) – Dion
您可以添加数组项的指标,正是如此不能有任何双打:
<?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));
结果:
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
)
)
编辑:如果你可以控制你的JSON的风格,你可以只产生这样说: [{"key":"value"},{"key","value"}]
,这样你可以跳过我的第二个解决方案的正则表达式部分
嗨,非常感谢,这真的帮了我很多:)我使用你的两个代码 – user2312198
在这里检查:http://stackoverflow.com/ques tions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object – Dekel