首页 > 解决方案 > 无法从 json_encoded 数组中获取数据

问题描述

我有关键和价值数据。键是一个数组。我想foreach这个关键数据。我使用 laravel 5。我的 json_decoded 数组如下:

Collection {#1288 ▼
  #items: array:4 [
    "{"id":1,"title":"abc","path":"abc-path"}" => 19
  ]
}

但我无法像我想要的代码那样获取关键数据:

@foreach($trendings as $key => $value)

 {{ $key->id }}

@endforeach

它给出了“尝试获取非对象的属性”错误。但是如果编写如下代码:

 @foreach($trendings as $key => $value)

     {{ $key }}

   @endforeach

它给了我

{"id":1,"title":"abc","path":"abc-path"}

但我希望它们在我的 html 中使用。我怎样才能拿到它们?

标签: phparraysjsonlaravel-5redis

解决方案


In my opinion, view should not execute a json_decode. I expect view to get already decoded variables, i think that if your data is not completly decoded something is missing before the view.

Of course i would rather work on fixing what's wrong before than patching your view.


推荐阅读