2016-11-08 59 views
0

更多信息重命名重複鍵: 我忘了,更不用說像列表項_...是我一直在使用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 
) 

感謝您的幫助。

+0

在這裏檢查:http://stackoverflow.com/ques tions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object – Dekel

回答

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

有一個數組重複密鑰(​​)是不可能的,因爲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 
) 
+0

嗨Dion,謝謝你的回答,實際上它很好用,但是項目生成ramdomly「list _...」 – user2312198

+0

可以嗎是否有可能修復重複的條目在JSON來自哪裏?問題是,json實際上不支持重複鍵,所以基本上你的json字符串是無效的... – Dion

+0

我有這樣的之前,$ tree ='list [Gentlemen] = null&list [Gold] = Gentlemen&list [Ladies] = NULL&列表[Plata的] =女子列表[金] =女士; – user2312198

0

感謝翁,唯一的事情是,我只有存取權限:

$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); 
+0

你可以添加如何將其轉換爲json的代碼?你需要什麼確切的數據從字符串?因爲在最後一個例子中(用[[Gentleman] [Gold] ...'),你不再有價值。 – Dion

+0

$ 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

+0

我更新了上面的文章,並在其中添加了代碼爲了更好的可讀性,但是我現在已經得到了你想要達到的效果,你只需要括號內的數值吧?順便說一下,你應該編輯你的實際問題帖子,而不是用更多細節添加一個答案給你自己的問題;) – Dion

2

您可以添加數組項的指標,正是如此不能有任何雙打:

<?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"}],這樣你可以跳過我的第二個解決方案的正則表達式部分

+0

嗨,非常感謝,這真的幫了我很多:)我使用你的兩個代碼 – user2312198