【问题标题】:Obtain all keys x from an array of objects (php, laravel blade)从对象数组(php,laravel刀片)中获取所有键x
【发布时间】:2019-09-16 16:08:28
【问题描述】:

数据库表如下所示:

数据库中的“附加”列存储了一个对象数组,如下所示:

[
{"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”的所有值。

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

橄榄 番茄酱 橄榄

【问题讨论】:

  • 你能试试{{ $op-&gt;extras }}吗?
  • 我猜你需要{{ $op-&gt;extra_type }}。

标签: php html laravel laravel-5 eloquent


【解决方案1】:

我解决了。我需要的是在遍历 json 对象数组时使用json_decode()。接下来,我提取了我需要的属性。

@foreach($item->orderedProducts as $op)
<tr>
    <td>
        @foreach(json_decode($op->extras, true) as $extra)
             {{ $extra['extra_name'] }}<br>
        @endforeach
    </td>
</tr>
@endforeach

【讨论】:

    【解决方案2】:

    我认为这会锻炼身体

    @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
    

    【讨论】:

    • ErrorException: foreach() 提供的参数无效
    • 您能否在第一个 foreach 中发布 $op 打印的内容...方法=> print_r($op)
    • 或者您可以尝试在第二次 foreach 中将 $op['extras'] 替换为 $op->extras
    • 我已经添加了 $op 返回的内容。你知道为什么json中有'\'吗?
    • 好的,它被视为一个json的字符串。无论如何,您必须在第二次 foreach 之前将“附加”格式化为 json 格式。那么你就完成了。欢呼
    【解决方案3】:

    你可以在 laravel 中使用-&gt; 访问对象值,试试这个

    @foreach($item->orderedProducts as $op)
    <tr>
     <td>
         <span>{{ $op->extra_name }}</span><br>
     </td>
    </tr>
    @endforeach
    

    【讨论】:

      【解决方案4】:
      @foreach($item->orderedProducts as $op)
      <tr>
          <td>
          @if ('Extras' == $op->extra_type)
               <span>{{ $op->extra_name }}</span><br>
          @endif
          </td>
      </tr>
      @endforeach
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多