简体   繁体   English

无法从json_encoded数组获取数据

[英]Cannot get data from json_encoded array

i have key and value datas. 我有键和值数据。 key is an array. 键是一个数组。 i want to foreach this key data. 我想介绍这个关键数据。 I use laravel 5. my json_decoded array like : 我使用laravel5。我的json_decoded数组如下:

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

But i can not fetch key data like i wanted my code : 但是我无法像我想要的代码那样获取关键数据:

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

 {{ $key->id }}

@endforeach

it gives ' Trying to get property of non-object ' error. 它给出“试图获取非对象的属性”错误。 but if write code like : 但是如果写像这样的代码:

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

     {{ $key }}

   @endforeach

it gives me 它给我

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

but i want them use in my html. 但我希望它们在我的HTML中使用。 how can i fetch them ? 我该如何获取它们?

In my opinion, view should not execute a json_decode. 我认为,视图不应执行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. 当然,我宁愿在修复问题之前而不是修补您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM