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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (67)

数据库表如下所示:

数据库中的“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": "[{\"id\":93,\"product_id\":8966,\"extra_type\":\"Extras\",\"extra_name\":\"Ketchup\",\"extra_price\":\"1.20\",\"extra_added\":true,\"price_sum\":1.2,\"extra_count\":1}]",
  "exclusions": "[{\"id\":117,\"product_id\":8966,\"extra_type\":\"Exclusions\",\"extra_name\":\"Rat poison\",\"extra_price\":null,\"exclusion_added\":true}]",
  "created_at": "2019-04-27 10:35:11",
  "updated_at": "2019-04-27 10:35:11",
  "product": {
    "id": 8966,
    "name": "Camera Brûlée",
    "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": "£11",
    "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"
    }
  }
}

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

所需的输出应该是这样的:

Olives Ketchup Olives

提问于
用户回答回答于

您可以使用->laravel 访问对象值,试试这个

@foreach($item->orderedProducts as $op)
<tr>
 <td>
     <span>{{ $op->extra_name }}</span><br>
 </td>
</tr>
@endforeach
用户回答回答于
@foreach($item->orderedProducts as $op)
<?php $array = stripslashes(json_encode($op->extras)) ?>
    <tr>
         <td>
             <span>
             @foreach($array as $value)
                 {{ $value['extra_name'] }}
             @endforeach
             </span><br>
         </td>
    </tr>
 @endforeach

扫码关注云+社区

领取腾讯云代金券