如何在多维阵列php中进行foreach循环值
我正在与Spotify API合作,我想对['tracks'] [0] [0] [“专辑”] [“ images”] [2] ['url']中的值进行foreach循环
。关于这个?
我试图做一个foreach循环,以检索多维关联阵列中关联的每个密钥的值,遍历每个元素,但仍然可以使其起作用。
Array
(
[tracks] => Array
(
[0] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/5yOvAmpIR7hVxiS6Ls5DPO
)
[href] => https://api.spotify.com/v1/artists/5yOvAmpIR7hVxiS6Ls5DPO
[id] => 5yOvAmpIR7hVxiS6Ls5DPO
[name] => Omah Lay
[type] => artist
[uri] => spotify:artist:5yOvAmpIR7hVxiS6Ls5DPO
)
)
[disc_number] => 1
[duration_ms] => 171764
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330802
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/5DS9LiyEdw2zY8bM6kjjgM
)
[href] => https://api.spotify.com/v1/tracks/5DS9LiyEdw2zY8bM6kjjgM
[id] => 5DS9LiyEdw2zY8bM6kjjgM
[is_local] =>
[is_playable] => 1
[name] => Infinity (feat. Omah Lay)
[popularity] => 73
[preview_url] => https://p.scdn.co/mp3-preview/5159fa17676fc96e32db13b7680f7497d5c2f74d?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 3
[type] => track
[uri] => spotify:track:5DS9LiyEdw2zY8bM6kjjgM
)
[1] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/68R39izwNAztATrXMOqkJS
)
[href] => https://api.spotify.com/v1/artists/68R39izwNAztATrXMOqkJS
[id] => 68R39izwNAztATrXMOqkJS
[name] => Bad Boy Timz
[type] => artist
[uri] => spotify:artist:68R39izwNAztATrXMOqkJS
)
)
[disc_number] => 1
[duration_ms] => 194000
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330809
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/558ULLj8yY2vT8XGtgY0q9
)
[href] => https://api.spotify.com/v1/tracks/558ULLj8yY2vT8XGtgY0q9
[id] => 558ULLj8yY2vT8XGtgY0q9
[is_local] =>
[is_playable] => 1
[name] => Loading (feat. Bad Boy Timz)
[popularity] => 64
[preview_url] => https://p.scdn.co/mp3-preview/7848a9f9ea879e6b1c6618508354e2559678aa8c?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 10
[type] => track
[uri] => spotify:track:558ULLj8yY2vT8XGtgY0q9
)
I'm working with Spotify API and I want to do a foreach loop for only the values in ['tracks'][0]["album"]["images"][2]['url']
How can I go about this?
I tried to do a foreach loop to retrieve value of each key associated inside the multidimensional associative array iterating through each element but can still get it to work.
Array
(
[tracks] => Array
(
[0] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/5yOvAmpIR7hVxiS6Ls5DPO
)
[href] => https://api.spotify.com/v1/artists/5yOvAmpIR7hVxiS6Ls5DPO
[id] => 5yOvAmpIR7hVxiS6Ls5DPO
[name] => Omah Lay
[type] => artist
[uri] => spotify:artist:5yOvAmpIR7hVxiS6Ls5DPO
)
)
[disc_number] => 1
[duration_ms] => 171764
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330802
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/5DS9LiyEdw2zY8bM6kjjgM
)
[href] => https://api.spotify.com/v1/tracks/5DS9LiyEdw2zY8bM6kjjgM
[id] => 5DS9LiyEdw2zY8bM6kjjgM
[is_local] =>
[is_playable] => 1
[name] => Infinity (feat. Omah Lay)
[popularity] => 73
[preview_url] => https://p.scdn.co/mp3-preview/5159fa17676fc96e32db13b7680f7497d5c2f74d?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 3
[type] => track
[uri] => spotify:track:5DS9LiyEdw2zY8bM6kjjgM
)
[1] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/68R39izwNAztATrXMOqkJS
)
[href] => https://api.spotify.com/v1/artists/68R39izwNAztATrXMOqkJS
[id] => 68R39izwNAztATrXMOqkJS
[name] => Bad Boy Timz
[type] => artist
[uri] => spotify:artist:68R39izwNAztATrXMOqkJS
)
)
[disc_number] => 1
[duration_ms] => 194000
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330809
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/558ULLj8yY2vT8XGtgY0q9
)
[href] => https://api.spotify.com/v1/tracks/558ULLj8yY2vT8XGtgY0q9
[id] => 558ULLj8yY2vT8XGtgY0q9
[is_local] =>
[is_playable] => 1
[name] => Loading (feat. Bad Boy Timz)
[popularity] => 64
[preview_url] => https://p.scdn.co/mp3-preview/7848a9f9ea879e6b1c6618508354e2559678aa8c?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 10
[type] => track
[uri] => spotify:track:558ULLj8yY2vT8XGtgY0q9
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会这样处理:
结果:
I would approach it like this:
Results in:
这是通过
array_walk_recursive
来方便地完成。在哪个深度或定位url
键/值的情况下,它将被发现并迭代。如下:请注意,如果您在不同的深度/位置上有多个
url
键,则将全部添加。如果您需要定位可能存在多个相同键的特定深度/位置,则该解决方案会更复杂。在您的用例中,这不是问题,所以我一直保持简单。另请注意,
array_walk_recursive
回调函数是仅在“叶子”或数组的最终值上调用的。因此,它将在包含数组的数组的成员上 。直到找到“叶子”直到找到“叶子”。This is conveniently accomplished with
array_walk_recursive
. It doesn't matter at what depth or position yoururl
key/value is, it will be found and iterated. As follows:Note that if you have multiple
url
keys at different depths/positions, they will all be added in. If you need to target a specific depth/position where multiple identical keys may exist, the solution would be a bit more complicated. In your use case this is not an issue, so I've kept it simple.Also note that
array_walk_recursive
callback function is only called on the "leaves" or the final values of an array. As such, it will not be called on members that are arrays containing arrays. Non-final array members will simply be followed onward until "leaves" are found.