Nitish Kumar
Nitish Kumar

Reputation: 6286

How to store values in array then use it as json

I'm trying to store values in an array structured like this:

{"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"}
                            ]},
            ]
}

}

I'm trying to place this in two different array and the joining them, but it is not getting the approrpiate result, I've used a variable which iterates to store the different values, if I don't use iteration it overlaps the values and only shows the last stored values. Following is my code:

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

Currently the data structure which I'm getting is in following format:

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 [▶]

I'm not able to fetch the data in this format, also after this I want to place the data to be used as json format. Help me out.

Upvotes: 0

Views: 56

Answers (2)

Bondan Sebastian
Bondan Sebastian

Reputation: 1016

I thinkyou should replace all $subuserinfo['child'][$i][$j++] = $subsubuserinfo; with $subuserinfo['child'][$j++] = $subsubuserinfo;

Upvotes: 1

Bart Bergmans
Bart Bergmans

Reputation: 4121

I see you are using Laravel? If you use return response()->json($tree); it will print JSON.

If you also want the parent in front of the JSON you can use this:

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

Upvotes: 0

Related Questions