猿问

从对象数组(php,laravel刀片)中获取所有键x

数据库表如下所示:

数据库中的“ extras”列存储着一个看起来像这样的对象数组:


[

{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"},


{"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"},


{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}

]

下面的代码(来自Laravel刀片)返回对象数组。


@foreach($item->orderedProducts as $op)

<tr>

     <td>

         <span>{{ $op['extras'] }}</span><br>

     </td>

</tr>

@endforeach

$ op返回以下内容:


{

  "id": 171,

  "product_id": 8966,

  "order_id": 175,

  "price": 11,

  "count": 1,

  "product_data": "{\"id\":8966,\"name\":\"Camera Br\\u00fbl\\u00e9e\",\"description\":\"Lorem ipsum dolor sit amet consectetur adipiscing elit etiam, conubia tempus sed dapibus augue gravida accumsan. Odio congue in blandit iaculis risus gravida parturient dictum quis rhoncus volutpat ornare tincidunt, dignissim ut pellentesque.\",\"price\":11,\"price_old\":null,\"category_id\":4584,\"created_at\":\"2019-03-17 14:59:56\",\"updated_at\":\"2019-04-17 16:37:03\",\"tax_group_id\":null,\"sort\":1,\"vendor_id\":null,\"option1\":null,\"option2\":null,\"option3\":null,\"option4\":null,\"option5\":null,\"option6\":null,\"images\":[\"http:\\/\\/localhost:8000\\/product_images\\/ElI9GImttc.jpg\"],\"formatted_price\":\"\\u00a311\",\"formatted_old_price\":\"0\",\"tax_value\":0,\"city_id\":null,\"restaurant_id\":null,\"product_images\":[{\"id\":9000,\"image\":\"\\/product_images\\/ElI9GImttc.jpg\",\"product_id\":8966,\"created_at\":\"2019-04-08 15:16:53\",\"updated_at\":\"2019-04-08 15:16:53\"}],\"tax_group\":null,\"category\":{\"id\":4584,\"name\":\"Random Things\",\"_lft\":1,\"_rgt\":2,\"parent_id\":null,\"created_at\":\"2018-11-09 13:15:01\",\"updated_at\":\"2019-04-17 16:47:21\",\"restaurant_id\":null,\"city_id\":null,\"category_image\":null,\"has_children\":0,\"image_url\":\"http:\\/\\/localhost:8000\\/category_images\\/a64be5a696402b0fe3649536ab6a49e4_1555519641.jpg\"},\"added\":true}",


我想要达到的目的是从“ extras:”获取(例如)“ extra_name”的所有值。


所需的输出应类似于:


橄榄番茄酱橄榄


阿晨1998
浏览 152回答 3
3回答

浮云间

我认为这将锻炼@foreach($item->orderedProducts as $op)<?php $array = stripslashes(json_encode($op->extras)) ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@foreach($array as $value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{{ $value['extra_name'] }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp; </tr>&nbsp;@endforeach

慕慕森

@foreach($item->orderedProducts as $op)<tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; @if ('Extras' == $op->extra_type)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span>{{ $op->extra_name }}</span><br>&nbsp; &nbsp; @endif&nbsp; &nbsp; </td></tr>@endforeach

函数式编程

您可以->在laravel中使用来访问对象的值,请尝试此操作@foreach($item->orderedProducts as $op)<tr>&nbsp;<td>&nbsp; &nbsp; &nbsp;<span>{{ $op->extra_name }}</span><br>&nbsp;</td></tr>@endforeach
随时随地看视频慕课网APP
我要回答