在 blade 中显示多维数组时出现了未定义偏移量 0 的错误,我无法使用 loop 来显示。
array:3 [▼
0 => array:6 [▼
0 => {#280 ▼
+"id": 30
+"category": "wedding"
+"sub_category": "groom"
+"image": "30.png"
}
1 => {#274 ▶}
2 => {#249 ▶}
3 => {#281 ▶}
4 => {#282 ▶}
5 => {#283 ▶}
]
1 => array:6 [▼
6 => {#284 ▶}
7 => {#285 ▶}
8 => {#286 ▶}
9 => {#287 ▶}
10 => {#288 ▶}
11 => {#289 ▶}
]
2 => array:6 [▼
12 => {#290 ▶}
13 => {#291 ▶}
14 => {#292 ▶}
15 => {#293 ▶}
16 => {#294 ▶}
17 => {#295 ▶}
]
]
我想从我的刀片上显示这个,这是我写的刀片代码,但它不起作用。
@for($i=0; $i<count($chunks); $i++)
<div class="column-grid">
@for($j=0; $j<count($chunks[$i]); $j++)
<div class="single-galleries-sub-img">
<div class="galleries-sub-img">
<img src="{{asset('img/galleries/gallery-images/'.$chunks[$i][$j]->image)}}"
alt="{{$chunks[$i][$j]->id}}">
</div>
<div class="galleries-sub-img-hvr">
<a href="{{asset('img/galleries/gallery-images/'.$chunks[$i][$j]->image)}}"
data-lightbox="image-4"><i class="fa fa-search-plus" aria-hidden="true"></i></a>
</div>
</div>
@endfor
</div>
@endfor
我在控制器中使用了这个函数
$galleries = DB::table('galleries')
->where('category','=',$request->type)
->paginate(18);
$chunks = $galleries->chunk(6);
$chunks = $chunks->toArray();
return view('galleries',compact('galleries','chunks'));
请帮帮我,如何才能在我的视图页面中显示这个数组。
在你的代码中,第一个数组可以正常工作,但是2 & 3数组就不行了,因为它使用的是 $chunks[$i][$j]
其中$j在2 & 3数组中不会得到0个元素,这就是为什么你得到的是 undefined error.
因为数组索引是继续在chunk中的,所以它不会在每个chunk中重置。
不使用for循环,你可以很容易地使用
foreach
循环,同时由于它是数组,所以最好总是使用foreach循环。
@foreach($chunks as $key => $value) {
<div class="column-grid">
@foreach($value as $k => $v) {
<div class="single-galleries-sub-img">
<div class="galleries-sub-img">
<img src="{{asset('img/galleries/gallery-images/'.$v->image)}}"
alt="{{$v->id}}">
</div>
<div class="galleries-sub-img-hvr">
<a href="{{asset('img/galleries/gallery-images/'.$v->image)}}"
data-lightbox="image-4"><i class="fa fa-search-plus" aria-hidden="true"></i></a>
</div>
</div>
@endforeach
</div>
@endforeach