dongquepao8653 2016-10-12 05:38
浏览 93
已采纳

如何删除laravel中foreach循环的最后一项中的逗号?

I tried adding last item logic using if condition, but the code is getting displayed.

My existing code :

<script type='application/ld+json'> 
{
  "itemListElement":
[
  @foreach($product->Offers as $offer)
   {
    "@type": "Offer",
    "name": "{{$offer->title}}",
    "price": "{{$offer->displayPrice}}",
   },  
   @endforeach 
 ]
}

}
</script>
  • 写回答

2条回答 默认 最新

  • drkjzk3359 2016-10-12 05:57
    关注

    Try to do this

    <?php $i = 1; $len = count($product->Offers); ?>
    @foreach($product->Offers as $offer)
      {
      "@type": "Offer",
      "name": "{{$offer->title}}",
      "price": "{{$offer->displayPrice}}",
      }<?php if($i < $len){echo ',';} $i++;?>
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?