Php 拉威尔-收藏。如何将数组分为两组
我创建了2个集合(数组),每个集合包含5个项目/事件的数组Php 拉威尔-收藏。如何将数组分为两组,php,wordpress,laravel,Php,Wordpress,Laravel,我创建了2个集合(数组),每个集合包含5个项目/事件的数组 $events = App\get_event_data( $args ) $collection = collect($events['events']); $event_chunks = $collection->chunk(5); $event_chunks->toArray(); @foreach ($event_chunks as $chunk) 上述各项的产出: object(Illuminate\Suppo
$events = App\get_event_data( $args )
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
@foreach ($event_chunks as $chunk)
上述各项的产出:
object(Illuminate\Support\Collection)[27632]
protected 'items' =>
array (size=2)
0 =>
object(Illuminate\Support\Collection)[27630]
protected 'items' =>
array (size=5)
...
1 =>
object(Illuminate\Support\Collection)[27631]
protected 'items' =>
array (size=5)
...
在我的下一个循环中,它只是逐个遍历数组的每个项。
我需要将这5个项目再分成两组:一组2人
三人小组
这样我就可以在每组周围绕一个div 电流回路:
@foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
@endforeach
@foreach($chunk as$key=>$event)
{{$event['title']}
@endforeach
我需要的是:
<div class="item-group">
[item1, item2]
</div>
<div class="item-group">
[item3, item4, item5]
</div>
[项目1、项目2]
[项目3、项目4、项目5]
完整代码:
{{-- Get all events --}}
@if( $events = App\get_event_data( $args ) )
@php
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
@endphp
@foreach ($event_chunks as $chunk)
<div class="{{ $block }}__flex-grid">
@foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
@endforeach
</div>
@endforeach
@endif
{{--获取所有事件--}
@如果($events=App\get\u event\u data($args))
@php
$collection=collect($events['events']);
$event_chunks=$collection->chunk(5);
$event_chunks->toArray();
@endphp
@foreach($event\u chunk作为$chunk)
@foreach($key=>$event的块)
{{$event['title']}
@endforeach
@endforeach
@恩迪夫
假设您始终有5项,解决方案可能是:
<div class="item-group">
@foreach (array_slice($chunk->toArray(),0,2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
@endforeach
</div>
<div class="item-group">
@foreach (array_slice($chunk->toArray(),2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
@endforeach
</div>
@foreach(数组\u切片($chunk->toArray(),0,2)作为$key=>$event)
{{$event['title']}
@endforeach
@foreach(数组\u切片($chunk->toArray(),2)作为$key=>$event)
{{$event['title']}
@endforeach
或者,如果希望避免代码重复:
@php $chunk = [array_slice($chunk->toArray(),0,2), array_slice($chunk,2)];
@foreach ($chunk as $group)
<div class="item-group">
@foreach ($group as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
@endforeach
</div>
@endforeach
@php$chunk=[array_-slice($chunk->toArray(),0,2),array_-slice($chunk,2)];
@foreach($chunk作为$group)
@foreach($key=>$event的组)
{{$event['title']}
@endforeach
@endforeach
如果您不知道或不确定是否有5个,则可能需要更改块/片的逻辑。我当前得到:
array\u slice()期望参数1为array,object given
,我转储了$chunk,它是一个对象数组,现在正在查看。已尝试$test=collect($chunk)$测试->toArray();var_dump($测试)代码>但没有变化。这是因为$chunk
是一个集合。更新为使用toArray()
Hey@felippe duarte。想知道如何才能单独更新div class=“item group”
?例如:第一组是div class=“item group--primary”
,第二组是div class=“item group--secondary”
@foreach($index=>chunk as$group)
<代码>如果($index==0)->主如果($index==1)->辅。。。