Nitish Kumar
Nitish Kumar

Reputation: 6276

How to iterate 2 dimensional array in laravel blade template engine

I'm trying to show data in blade template engine in laravel 5.3. I'm having a 2 dimensional array of following format while doing 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 [▶]

I'm trying to fetch this data like this:

 <div class="tree-content">
<!-- Level 1 -->
<div class="level-block">
    <div class="levels level-1">
        <span class="{{ $tree['class'] }}"><i class="fa fa-user"></i></span>
        <div class="mem-info">
            <span class="bold">{{ $tree['user_name'] }}</span>
            <span>{{ $tree['user_loc'] }}</span>
            <span>ID: {{ $tree['user_id'] }}</span>
        </div>
    </div>
</div>
<!-- Level 2 -->
<div class="level-block">
    <div class="levels level-2-m5">

        @foreach($tree['child'] as $child)

            <div class="member-block {{ $child['user_blockclass'] }}">
                <span class="{{ $child['class'] }}"><i class="fa fa-user"></i></span>
                <div class="mem-info">
                    <span class="bold">{{ $child['user_name'] }}</span>
                    <span>{{ $child['user_loc'] }}</span>
                    <span>ID: {{ $child['user_id'] }}</span>
                </div>
            <!-- Level 3 Starts -->
                <div class="level-block">
                    <div class="levels level-3-m5">
                    @foreach($child['child'] as $subchild)

                        <div class="member-block {{ $subchild['user_blockclass'] }}">
                            <span class="{{ $subchild['class'] }}"><i class="fa fa-user"></i></span>
                            <div class="mem-info mem-hover">
                                <span class="bold">{{ $subchild['user_name'] }}</span>
                                <span>ID {{ $subchild['user_id'] }}</span>
                                <a href="#">More Details</a>
                            </div>  
                        </div>

                    @endforeach
                    </div>
                </div>
                <!-- Level 3 Ends here -->
            </div>

        @endforeach
    </div>
</div>

I'm unable to fetch the values, I guess I'm doing wrong in iteration as of now I'm getting error of undefined index. Help me out. Thanks

Upvotes: 3

Views: 708

Answers (2)

Rohit shah
Rohit shah

Reputation: 819

As you have array inside the Sub child either you'll have to use one more foreach loop Because if more child gets added

     array:6 [▼
      "class" => "Green"
      "user_name" => "Nitish"
      "user_loc" => "Delhi"
      "user_id" => 1
      "user_blockclass" => null
      "child" => array:4 [▼<-- Using $tree['child'] 
          0 => array:6 [▼
              "class" => "Green"
              "user_name" => null
              "user_loc" => null
              "user_id" => 1
              "user_blockclass" => "fst"
              "child" => array:1 [▼<-- Using $child['child']
                   0 => array:5 [▼<-- If this index increase You'll need one more foreach else @foreach($child['child'][0] as $subchild)
this should work
                       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 [▶]

or Use

@foreach($child['child'][0] as $subchild)

Hope this works

Upvotes: 2

aynber
aynber

Reputation: 23011

If you look at this:

     "child" => array:1 [▼
           0 => array:5 [▼  < -- subarray
               0 => array:5 [▼ <-- sub-sub-arrays
                 "class" => "Green"
                 "user_name" => "pandey"
                 "user_loc" => "sdgfsjd"
                 "user_id" => 6
                 "user_blockclass" => "fst"

It's in a sub-sub-array. So you need to add an additional array key

@foreach($child['child'][0] as $subchild)

Upvotes: 0

Related Questions