如何在数组中存储值,然后将其用作json

时间:2016-12-01 15:25:40

标签: php arrays laravel

我试图将值存储在如下结构的数组中:

{"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结果,我已经使用了一个迭代存储不同值的变量,如果我不&# 39; t使用迭代它重叠值,只显示最后存储的值。以下是我的代码:

$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格式。帮助我。

2 个答案:

答案 0 :(得分:1)

我认为您应该将所有$subuserinfo['child'][$i][$j++] = $subsubuserinfo;替换为$subuserinfo['child'][$j++] = $subsubuserinfo;

答案 1 :(得分:0)

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

如果您还想在JSON前面使用parent,可以使用:

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