首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json_decode只返回集合的第一个成员

json_decode只返回集合的第一个成员
EN

Stack Overflow用户
提问于 2018-02-19 23:41:41
回答 1查看 33关注 0票数 0

我在一个关联数组中存储了一些JSON字符串。我可以很好地访问这些字符串,但是json_decode只是提取每个集合的最后一个成员,而完全忽略了最后一个集合。我做错了什么?谢谢。

这段代码

代码语言:javascript
复制
foreach ($arr_data as $row) {
    echo $row['tags'];
    print_r (json_decode($row['tags'], TRUE));      
}

输出本

代码语言:javascript
复制
{"bun_tag": "punctuation"},{"bun_tag": "quotation marks"},{"bun_tag": "document heading"},{"bun_tag": "document structure"}{"bun_tag": "multiple inversion"},{"bun_tag": "overloaded compound"},{"bun_tag": "syntactic ambiguity"},{"bun_tag": "excessive syntactic distance"}{"bun_tag": "omission"},{"bun_tag": "referential distortion"}{"bun_tag": "tense"},{"bun_tag": "modality"},{"bun_tag": "nominalisation"},{"bun_tag": "directive infinitive"}{"bun_tag": "good"}

Array
(
    [bun_tag] => good
)

{"bun_tag": "garden path"},{"bun_tag": "overloaded compound"}{"bun_tag": "missing determiner"},{"bun_tag": "referential ambiguity"}{"bun_tag": "garden path"}

Array
(
    [bun_tag] => garden path
)

{"bun_tag": "overloaded compound"}

Array
(
    [bun_tag] => overloaded compound
)

{"bun_tag": "capitalisation"},{"bun_tag": "title of document section"}{"bun_tag": "syntactic ambiguity"},{"bun_tag": "excessive syntactic distance"}{"bun_tag": "selectional restriction"}

Array
(
    [bun_tag] => selectional restriction
)

{"bun_tag": "garden path"},{"bun_tag": "overloaded compound"},{"bun_tag": "syntactic ambiguity"}{"bun_tag": "relational ambiguity"},{"bun_tag": "containment relationship"}{"bun_tag": "punctuation"},{"bun_tag": "weak interruption"}{"bun_tag": "meaning unclear"},{"bun_tag": "domain terminology"}{"bun_tag": "homonymy"},{"bun_tag": "nominalisation"},{"bun_tag": "meaning unclear"},{"bun_tag": "domain terminology"},{"bun_tag": "referential ambiguity"}{"bun_tag": "document structure"}

Array
(
    [bun_tag] => document structure
)

{"bun_tag": "polysemy"},{"bun_tag": "agent / receiver"},{"bun_tag": "relational distortion"}
EN

回答 1

Stack Overflow用户

发布于 2018-02-19 23:54:23

根据Siguza的评论,JSON字符串的格式不太好。通过在两端添加方括号来解决问题,如下所示

代码语言:javascript
复制
foreach ($arr_data as $row) {
    echo $row['tags'];
    print_r (json_decode('[' . $row['tags'] . ']', TRUE));      
}

这会将输入字符串转换为有效的数组。

谢谢你的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48875826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档