gpt4 book ai didi

php - 访问已解码的 json 字符串 PHP 中的字段

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:33 25 4
gpt4 key购买 nike

我正在尝试使用 php 访问这个已解码的 json 字符串中第一首歌曲的字段“id”。我已经尝试了所有可能的组合,例如:

$response->歌曲[0]->id

这是我解码后的 json 字符串:

   Array (
[response] => Array (
[status] => Array (
[version] => 4.2
[code] => 0
[message] => Success
)
[songs] => Array (
[0] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOKGWES13D647BE466
[artist_name] => Kanye West
[title] => All Of The Lights
)
[1] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOHBKVU14509A9F6C3
[artist_name] => Kanye West
[title] => All Of The Lights
)
[2] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SODELAY13AD1ACC8CF
[artist_name] => Kanye West
[title] => All Of The Lights
)
[3] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOUDIYM14B7C7B2D95
[artist_name] => Kanye West
[title] => All of the Lights
)
[4] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOTEMPJ13DB921F71F
[artist_name] => Kanye West
[title] => All of the Lights (
Remix
)
)
[5] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOXIDRL13CCFBBC829
[artist_name] => Kanye West
[title] => All Of The Lights
[LbLuke Rmx]
)
[6] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOTJZSO12D857905F6
[artist_name] => Kanye West
[title] => All Of The Lights (
Interlude
)
)
[7] => Array (
[artist_id] => ARVCDGF12FE08689BA
[id] => SOGLUJD130516E0D00
[artist_name] => Made famous by Kanye West
[title] => All of the lights
)
)
)
)

提前致谢

最佳答案

$id = $json_array['response']['songs'][0]['id'];

解释

看看响应,你的响应是一个多维数组,这意味着你有一个由几个数组组成的数组,每个数组可以包含一个或多个数组。

在第一个数组中你有“response”,这个包含了其余的,所以..

$id = $json_array['response']

你必须从这个数组中嵌套,直到你得到你想要的元素。其中包含到另一个数组歌曲中,所以..

$id = $json_array['response']['songs']

这个有几个由数字索引的元素,因为你想要第一首歌曲的 id,我们选择 0 元素,

$id = $json_array['response']['songs'][0]

之后你可以得到你想要的元素:

$id = $json_array['response']['songs'][0]['id'];

关于php - 访问已解码的 json 字符串 PHP 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com