2016-12-01 51 views
0

我試圖存儲值結構像這樣的數組:如何值存儲在陣列,然後用它作爲JSON

{"parent": 
{"class":"Green","user_name":"Nitish","user_loc":"Delhi","user_id":1,"user_blockclass":null, 
    "child":[ 
       {"class":"Green","user_name":null,"user_loc":null,"user_id":1,"user_blockclass":"fst", 
        "child":[ 
           {"class":"Green","user_name":"pandey","user_loc":"sdgfsjd","user_id":6,"user_blockclass":"fst"}, 
           {"class":"Green","user_name":"chaku","user_loc":"sdgjs","user_id":7,"user_blockclass":"snd"}, 
           {"class":"Green","user_name":"iks","user_loc":"sjkdfhkjs","user_id":8,"user_blockclass":"trd"}, 
           {"class":"Green","user_name":"yash","user_loc":"hfksjdhfk","user_id":9,"user_blockclass":"frt"}, 
           {"class":"Green","user_name":"joshi","user_loc":"dsfh","user_id":10,"user_blockclass":"fth"} 
          ]}, 
       {"class":"Green","user_name":null,"user_loc":null,"user_id":1,"user_blockclass":"snd", 
        "child":[ 
           {"class":"Green","user_name":"pandey","user_loc":"sdgfsjd","user_id":6,"user_blockclass":"fst"}, 
           {"class":"Green","user_name":"chaku","user_loc":"sdgjs","user_id":7,"user_blockclass":"snd"}, 
           {"class":"Green","user_name":"iks","user_loc":"sjkdfhkjs","user_id":8,"user_blockclass":"trd"}, 
           {"class":"Green","user_name":"yash","user_loc":"hfksjdhfk","user_id":9,"user_blockclass":"frt"}, 
           {"class":"Green","user_name":"joshi","user_loc":"dsfh","user_id":10,"user_blockclass":"fth"} 
          ]}, 
      ] 
} 

}

我試圖把這個在兩個不同數組和加入它們,但它沒有得到approrpiate結果,我使用了一個迭代來存儲不同值的變量,如果我不使用迭代它會重疊值並且只顯示最後存儲的值。以下是我的代碼:

$blockclass = ['fst', 'snd', 'trd', 'frt', 'fth']; 
$i = 0; 

$children = $user->relations()->wherePlanId($selectplan)->get(); 
foreach($children as $ch) 
{ 
    $j = 0; 
    $subuserinfo = []; 
    $parent_id = $ch->pivot->child; 
    $subuser = User::find($ch->pivot->child); 
    $subuserinfo['class'] = "Green"; 
    $subuserinfo['user_name'] = $ch->name; 
    $subuserinfo['user_loc'] = $ch->city; 
    $subuserinfo['user_id'] = $ch->id; 
    $subuserinfo['user_blockclass'] = $blockclass[$i]; 
    if($subuser){ 
     $subchildren = $subuser->relations()->wherePlanId($selectplan)->get(); 
     foreach($subchildren as $subchild) 
     { 
      $subsubuser = User::find($subchild->pivot->child); 
      $subsubuserinfo['class'] = "Green"; 
      $subsubuserinfo['user_name'] = $subsubuser->name; 
      $subsubuserinfo['user_loc'] = $subsubuser->city; 
      $subsubuserinfo['user_id'] = $subsubuser->id; 
      $subsubuserinfo['user_blockclass'] = $blockclass[$j]; 
      $subuserinfo['child'][$i][$j++] = $subsubuserinfo; 
     } 
    } 
    else 
    { 
     $subuserinfo['child'][$i][$j++] = NULL; 
    }    
    $userinfo['child'][$i++] = $subuserinfo; 
} 
$tree = $userinfo; 
dd($tree); 

目前這我得到的數據結構是採用以下格式:

array:6 [▼ 
    "class" => "Green" 
    "user_name" => "Nitish" 
    "user_loc" => "Delhi" 
    "user_id" => 1 
    "user_blockclass" => null 
    "child" => array:4 [▼ 
     0 => array:6 [▼ 
      "class" => "Green" 
      "user_name" => null 
      "user_loc" => null 
      "user_id" => 1 
      "user_blockclass" => "fst" 
      "child" => array:1 [▼ 
       0 => array:5 [▼ 
        0 => array:5 [▼ 
        "class" => "Green" 
        "user_name" => "pandey" 
        "user_loc" => "sdgfsjd" 
        "user_id" => 6 
        "user_blockclass" => "fst" 
        ] 
        1 => array:5 [▼ 
         "class" => "Green" 
         "user_name" => "chaku" 
         "user_loc" => "sdgjs" 
         "user_id" => 7 
         "user_blockclass" => "snd" 
        ] 
        2 => array:5 [▼ 
         "class" => "Green" 
         "user_name" => "iks" 
         "user_loc" => "sjkdfhkjs" 
         "user_id" => 8 
         "user_blockclass" => "trd" 
        ] 
       ] 
     ] 
] 
1 => array:6 [▶] 
2 => array:6 [▶] 
3 => array:6 [▶] 

我不能這樣我以後在這個格式來獲取數據,也希望將數據作爲json格式使用。幫幫我。

回答

1

我thinkyou應該更換所有$subuserinfo['child'][$i][$j++] = $subsubuserinfo;$subuserinfo['child'][$j++] = $subsubuserinfo;

0

我看到你在使用Laravel?如果您使用return response()->json($tree);它將打印JSON。

如果您還想要parent在JSON的前面,你可以使用這個:

return response()->json(['parent' => $tree]); 
+0

我面臨的困難與數據格式不來適當的。 –

+0

你能打印出你當前的JSON數據格式和你想要的JSON數據格式嗎? –

+0

它已經在問題的開始,我正在尋找的json格式,最後陣列格式是我得到的值。 –

相關問題