gpt4 book ai didi

php - 如何在 PHP/Blade for Laravel 中使用 Foreach 循环?

转载 作者:行者123 更新时间:2023-12-01 16:13:54 25 4
gpt4 key购买 nike

这是一个简单的问题:有什么方法可以使用 foreach 循环或类似的东西来优化此代码吗?

@if(isset($tags[0]))
<span class="label label-primary">{{ $tags[0] }}</span>
@endif
@if(isset($tags[1]))
<span class="label label-primary">{{ $tags[1] }}</span>
@endif
@if(isset($tags[2]))
<span class="label label-primary">{{ $tags[2] }}</span>
@endif
@if(isset($tags[3]))
<span class="label label-primary">{{ $tags[3] }}</span>
@endif
@if(isset($tags[4]))
<span class="label label-primary">{{ $tags[4] }}</span>
@endif
@if(isset($tags[5]))
<span class="label label-primary">{{ $tags[5] }}</span>
@endif
@if(isset($tags[6]))
<span class="label label-primary">{{ $tags[6] }}</span>
@endif
@if(isset($tags[7]))
<span class="label label-primary">{{ $tags[7] }}</span>
@endif
@if(isset($tags[8]))
<span class="label label-primary">{{ $tags[8] }}</span>
@endif
@if(isset($tags[9]))
<span class="label label-primary">{{ $tags[9] }}</span>
@endif

谢谢您,祝您度过愉快的一天/夜晚!

最佳答案

你可以简单地这样做:

@foreach ($tags as $tag)
<span class="label label-primary">{{ $tag }}</span>
@endforeach

关于php - 如何在 PHP/Blade for Laravel 中使用 Foreach 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com