如何在循环中将数据存储在数组中

时间:2016-12-01 18:03:01

标签: php arrays json multidimensional-array

我试图获得格式应该是这样的数据:

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

每个元素将包含5个子元素,每个子元素将分别包含5个子元素。现在我试图将数据推送到数组中:

public function viewplans(Request $request)
{
    $selectplan = $request->selectplan;
    $user = Auth::user();
    $userinfo= [];
    $userinfo['class'] = "Green";
    $userinfo['user_name'] = $user->name;
    $userinfo['user_loc'] = $user->city;
    $userinfo['user_id'] = $user->id;
    if($selectplan == 1)
    {                                   
        $blockclass = ['fst', 'snd', 'trd', 'frt', 'fth'];            
        $children = $user->relations()->wherePlanId($selectplan)->get();
        $subuserinfo = [];
        $subsubuserinfo = [];
        for($i = 0; $i <5; $i++)
        {

          if($children[$i])
          {
            $subuser = User::findOrFail($children[$i]->pivot->child);
            $subuserinfo['class'] = "Green";
            $subuserinfo['user_name'] = $subuser->name;
            $subuserinfo['user_loc'] = $subuser->city;
            $subuserinfo['user_id'] = $subuser->id;
            $subuserinfo['user_blockclass'] = $blockclass[$i];
            $subchildren = $subuser->relations()->wherePlanId($selectplan)->get();
            for($j = 0; $j < 5; $j++)
            {

                if($subchildren[$j]){
                  $subsubuser = User::findOrFail($subchildren[$j]->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'][$j] = $subsubuserinfo;
                }
                else
                {
                  $subsubuserinfo['class'] = "Black";
                  $subsubuserinfo['user_name'] = 'No User';
                  $subsubuserinfo['user_loc'] = 'No Loc';
                  $subsubuserinfo['user_id'] = 'No ID';
                  $subsubuserinfo['user_blockclass'] = $blockclass[$j];
                  $subuserinfo['child'][$j] = $subsubuserinfo;
                }
            }
            $userinfo['child'][$i] = $subuserinfo;                                                                    
          }
          else
          {
              $subsubuserinfo['class'] = "Black";
              $subsubuserinfo['user_name'] = 'No User';
              $subsubuserinfo['user_loc'] = 'No Loc';
              $subsubuserinfo['user_id'] = 'No ID';
              $subsubuserinfo['user_blockclass'] = $blockclass[$i];
              $userinfo['child'][$i] = $subuserinfo;
          }
        }
        $tree = $userinfo;
//      dd($tree);
        return view('member.5matrix', [
          'tree' => ($tree),
          'blockclass' => $blockclass
        ]);
    }
}

即使数据不可用,也应存储else值。目前我收到错误:

  

未定义的偏移量:2

帮助我以JSON格式提及正确格式的数据。

1 个答案:

答案 0 :(得分:1)

您显示的代码中唯一可能导致“未定义的偏移量:2”错误的位置

$subuser = User::findOrFail($children[$i]->pivot->child);

$subsubuser = User::findOrFail($subchildren[$j]->pivot->child);

使用整数($i$j)作为数组键的其他任何地方都要么在该键处设置一个值,这不会导致未定义的偏移量错误(它只会创建key(如果它不存在),或引用$blockclass,它具有足够的元素,使for循环不会超过其长度。

这意味着无论出于何种原因,

$children = $user->relations()->wherePlanId($selectplan)->get();

$subchildren = $subuser->relations()->wherePlanId($selectplan)->get();

并没有像您期望的那样返回任意数量的项目。 (更具体地说,它似乎只返回两个而不是五个。)

可能有其他方法可以执行此操作,但如果您希望按照当前的方式继续操作,则需要在使用之前添加$children[$i]$subchildren[$j]设置的检查它们,如果不是,则添加默认值。

if ($subuser) {...if ($subsubuser) {...检查这些变量是否已设置,但您实际上是在尝试设置它们时收到错误。我认为您实际上可以将findOrFail调用与$children[$i]条件中$subchildren[$j]if的检查结合起来:

if (isset($children[$i]) && $subuser = User::findOrFail($children[$i]->pivot->child)) {