Laravel阵列循环

时间:2018-01-18 09:52:11

标签: php laravel

我有运输方式,将计算价格,此代码/价格等来自第三方网站(没有存储在我的数据库中)。

  

我的问题是我不知道如何获得我需要的物品!

这是dd

上我的数据的屏幕截图

screenshot1

我的function

public function index() {
        $province = RajaOngkir::Provinsi()->all();

       // this part will return dd image above
        $cost = RajaOngkir::Cost([
            'origin'        => 501, // id kota asal
            'destination'   => 114, // id kota tujuan
            'weight'        => 1000, // berat satuan gram
            'courier'       => 'jne', // kode kurir pengantar ( jne / tiki / pos )
        ])->get();
        return view('welcome', compact('province', 'cost'));
    }

我的观看代码blade

@foreach($cost as $option)
  {{$option['code']}} <br>
  {{$option['name']}}
@endforeach

PS:我的循环中有dd {{dd($option)}}(@foreach)

到目前为止,我的循环返回了这个(与dd相比):

screenshot2

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

  @if(!empty($cost))
    @foreach($cost as $option)
          {{$option['code']} <br>
          {{$option['name']}} <br>
           @if(!empty($option['costs']))
               @foreach($option['costs'] as $cost)
                     {{$cost['service']}} <br>
                     {{$cost['description']}} <br>
                     @if(!empty($cost['cost'])
                         @foreach($cost['cost'] as $c)
                             {{ $c['value'] }} <br>
                             {{ $c['etd'] }}  <br>     
                             {{ $c['note'] }}   <br>
                         @endforeach 
                     @endif            
              @endforeach
          @endif
    @endforeach
 @endif

答案 1 :(得分:0)

试试这个:

 @foreach($cost as $option)
      {{$option['code']}} <br>
      {{$option['name'] }} <br>
       @foreach($option['costs'] as $cost)
            @foreach($cost as $arr)

             {{$arr['service'] }} <br>
             {{$arr['description']}} <br>
               @foreach($arr['cost'] as $c)
                    {{ $c['value'] }} <br>
                    {{ $c['etd'] }}  <br>     
                    {{ $c['note'] }}   <br>
              @endforeach
            @endforeach  

      @endforeach
@endforeach