Php 从数组中获取属性并替换
我有这个阵列:Php 从数组中获取属性并替换,php,arrays,laravel,Php,Arrays,Laravel,我有这个阵列: 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) {
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)
}
}
我在页面上这样显示
@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
我想做的是在交货是免费的情况下以某种方式进行替换,例如[shipping]=>[price]=0以显示0。像这样:
Test Product 1 $122 / €113
Free Delivery $0 / €0
我可以这样访问它
<?php $shipping = $order->getOrderData($order->order_details)->shipping->price; ?>
在这里,我们检查数组是否为发货项目,并且价格设置为零。如果是这样,我们显示的是零欧元,否则我们显示的是实际成本。这很令人困惑吗?在foreach循环中使用一个简单的if可以吗?@SaravananSampathkumar,我已经用我尝试过的if条件更新了我的问题,但仍然是sameNo,我的意思是检查价格是否为零,比如@if$item->price,那么它是免费交付的,否则是正常的。是的,这是我所问问题的解决方法。我发现这里还缺少一些东西。谢谢,但是送货可以不同于零。。付了一个。这意味着上述代码将再次显示总欧元
<?php $shipping = $order->getOrderData($order->order_details)->shipping->price; ?>
${{ $item->price * $item->quantity }}
@if( ! $order->getOrderData($order->order_details)->shipping)
{{ $order->getOrderData($order->order_details)->shipping->price }}
@else
€{{ $order['total_eur'] }}
@endif
@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