我正在尝试在laravel 5.3中显示刀片模板引擎中的数据。在做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 [▶]
我正试图像这样获取这些数据:
<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>
我无法获取值,我猜我在迭代中做错了,因为我现在得到undefined index
的错误。帮帮我。感谢
答案 0 :(得分:2)
由于您在Sub子项中有数组,您将不得不再使用一个foreach循环因为如果添加更多子项
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 [▶]
或使用
@foreach($child['child'][0] as $subchild)
希望这有效
答案 1 :(得分:0)
如果你看一下:
"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"
它在一个子子阵列中。所以你需要添加一个额外的数组键
@foreach($child['child'][0] as $subchild)