我正在使用laravel框架,我有一个嵌套数组,可以在其中显示结果的子集.例如,在下面的数组中,我显示每年多个任务.
Array
(
[0] => years_tasks_section_grid
(
[0] => 2022
[1] => tasks_grid
(
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
[3] => Task4 2022
[4] => Task5 2022
[5] => Task6 2022
)
)
[1] => years_tasks_section_grid
(
[0] => 2021
[1] => tasks_grid
(
[0] => Task1 2021
[1] => Task2 2021
[2] => Task3 2021
)
)
)
The problem I want to display the last 3 tasks from the 2nd level of nested array.基本上,基于我的例子I try to get this (only tasks of 2022):
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
但我得到了这个(基本上,我得到了2022年和2021年的三个最后任务,可能我还会有future 几年的其他任务):
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
[0] => Task1 2021
[1] => Task2 2021
[2] => Task3 2021
这是我在Laravel Blade中的代码(我使用了ARRAY_SLICE,但不起作用,因为我得到了上面的结果):
@foreach($sections['years_tasks_section_grid'] as $year)
@foreach(array_slice($year['tasks_grid'], 0, 5) as $task)
<div class="col-md-4 col-xl-2 d-flex align-items-center justify-content-center justify-content-md-start mb-7 mb-xl-0 px-0">
// Other code
</div>
@endforeach
@endforeach
有什么主意吗?