【发布时间】:2018-10-09 16:20:29
【问题描述】:
我有键和值数据。键是一个数组。我想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 中使用。我怎样才能获取它们?
【问题讨论】:
-
你的
$key是 json 字符串。您需要json_decode($key)才能访问字段。但更合理的方法——是修复你的收藏。为什么json字符串是key而不是value? -
这是 json 解码数据。这是redis数据,redis不允许我使用值
-
你可能不理解,但我可以重复一遍:
{"id":1,"title":"abc","path":"abc-path"}是一个 json 字符串。要访问属性,您必须json_decode它。 -
那么然后解码
$trendings的密钥。 -
尝试
$trendings =[ '{"id":1,"title":"abc","path":"abc-path"}' => 19 ]; foreach($trendings as $key => $value) { echo json_decode($key)->id; }使用json_decode
标签: php arrays json laravel-5 redis