douyu4822 2017-03-22 10:29
浏览 42
已采纳

从数组获取属性并替换

I have this array:

object(stdClass)#458 (2) {
  ["1"]=>
  object(stdClass)#456 (3) {
    ["title"]=>
    string(3) "Test Product 1"
    ["quantity"]=>
    int(1)
    ["price"]=>
    int(1221)
  }
  ["shipping"]=>
  object(stdClass)#457 (3) {
    ["quantity"]=>
    int(1)
    ["title"]=>
    string(13) "Free Delivery"
    ["price"]=>
    int(0)
  }
}

I'm displaying this on the page like this.

@foreach($order->getOrderData($order->order_details) as $itemId => 
     <?php $total += ($item->price * $item->quantity); ?>
     <h4 class="media-heading">{{ $item->title }}</h4>
      {{ $item->quantity }}
      ${{ $item->price * $item->quantity }} / €{{ $order['total_eur'] }}
@endforeach

So far everything works perfect except the addition value which I call from database $order['total_eur']. This value is saved when order is placed so I then show total in USD and total in EUR.

The problem here is that ${{ $item->price * $item->quantity }} contain also shipping and when shipping is Free e.g. $ 0 is shown on page

Test Product 1   $122 /  €113      
Free Delivery    $0 / €113 

What I'm trying to do is to replace somehow when the delivery is free e.g. ["shipping"]=>["price"] = 0 to show 0 instead. to be like this:

Test Product 1   $122 /  €113      
Free Delivery    $0 / €0 

I can access it like this

<?php $shipping = $order->getOrderData($order->order_details)->shipping->price; ?>

But I can't figured it out how can I change it in the foreach loop.

I hope I explained this clear and understandable. Can anyone suggest something that may work?

Update: I've tried this simple if inside foreach but still showing both eur price on both

${{ $item->price * $item->quantity }} 
  @if( ! $order->getOrderData($order->order_details)->shipping) 
      {{ $order->getOrderData($order->order_details)->shipping->price }} 
  @else 
      €{{ $order['total_eur'] }} 
  @endif
  • 写回答

1条回答 默认 最新

  • dongmei8209 2017-03-22 10:51
    关注
    @foreach($order->getOrderData($order->order_details) as $itemId => $item)
         <?php $total += ($item->price * $item->quantity); ?>
         <h4 class="media-heading">{{ $item->title }}</h4>
          {{ $item->quantity }}
          @if ($itemId == "shipping" && $item->price == 0) 
            $0 / €0
          @else
            ${{ $item->price * $item->quantity }} / €{{ $order['total_eur'] }}
          @endif
    @endforeach
    

    Here we are checking the array whether it is shipping item and price is set to zero. If so we are displaying zero €, else we are displaying the actual cost.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?