如何使用PHP从此数组文件访问数据?

时间:2018-11-17 10:24:20

标签: php arrays json

这是使用var_export($result);

的数据
 array ( 0 => array ( 'album' => array ( 'album_type' => 'ALBUM', 'artists' => array ( 0 => array ( 'external_urls' => array ( 'spotify' => 'https://open.spotify.com/artist/6rBvjnvdsRxFRSrq1StGOM', ), 'href' => 'https://api.spotify.com/v1/artists/6rBvjnvdsRxFRSrq1StGOM', 'id' => '6rBvjnvdsRxFRSrq1StGOM', 'name' => 'Bathory', 'type' => 'artist', 'uri' => 'spotify:artist:6rBvjnvdsRxFRSrq1StGOM', ), ), 'external_urls' => array ( 'spotify' => 'https://open.spotify.com/album/1ZN0dVt3JGTUJZ5TydY1E8', ), 'href' => 'https://api.spotify.com/v1/albums/1ZN0dVt3JGTUJZ5TydY1E8', 'id' => '1ZN0dVt3JGTUJZ5TydY1E8', 'images' => array ( 0 => array ( 'height' => 640, 'url' => 'https://i.scdn.co/image/ba052dff3d3cf0f5bd9b66a7ac707c2f4dfad065', 'width' => 640, ), 1 => array ( 'height' => 300, 'url' => 'https://i.scdn.co/image/20b02fb5aae2cfe790adb6b65433850a0977542c', 'width' => 300, ), 2 => array ( 'height' => 64, 'url' => 'https://i.scdn.co/image/668e5fb3a1044d4a2d7a90d3491612fe702d558b', 'width' => 64, ), ), 'name' => 'Blood Fire Death', 'type' => 'album', 'uri' => 'spotify:album:1ZN0dVt3JGTUJZ5TydY1E8', ), 'artists' => array ( 0 => array ( 'external_urls' => array ( 'spotify' => 'https://open.spotify.com/artist/6rBvjnvdsRxFRSrq1StGOM', ), 'href' => 'https://api.spotify.com/v1/artists/6rBvjnvdsRxFRSrq1StGOM', 'id' => '6rBvjnvdsRxFRSrq1StGOM', 'name' => 'Bathory', 'type' => 'artist', 'uri' => 'spotify:artist:6rBvjnvdsRxFRSrq1StGOM', ), ), 'disc_number' => 1, 'duration_ms' => 220200, 'explicit' => false, 'external_ids' => array ( 'isrc' => 'SEXHQ8800404', ), 'external_urls' => array ( 'spotify' => 'https://open.spotify.com/track/0i6joqNGcGa73MXEi1qVJi', ), 'href' => 'https://api.spotify.com/v1/tracks/0i6joqNGcGa73MXEi1qVJi', 'id' => '0i6joqNGcGa73MXEi1qVJi', 'is_playable' => true, 'name' => 'Pace Till Death', 'popularity' => 28, 'preview_url' => 'https://p.scdn.co/mp3-preview/572b24bf4fae0a7ce5ef835b5dc5c60826c4bc79?cid=24974f52d3fc4029a03bee338698b062', 'track_number' => 4, 'type' => 'track', 'uri' => 'spotify:track:0i6joqNGcGa73MXEi1qVJi', ), )

如何访问下面当前文件的这一部分

[images] => Array ( [0] => Array ( [height] => 640 [url] => https://i.scdn.co/image/f57171446c8d61050af0e43726c4863dfdc235a4 [width] => 640 ) [1] => Array ( [height] => 300 [url] => https://i.scdn.co/image/4111d12bb7e3a142d12309ba699ce97d78fd8905 

使用PHP如何在上方显示此数据,我在下面尝试了检索名称的方法,但是,我想要上面显示的图像。请注意,$ result是我存储API结果的地方

$result = json_decode(json_encode($result), true);

foreach($result as $obj){

   echo "Name : " . $obj['name'];
}

2 个答案:

答案 0 :(得分:0)

images数组不在declare @claims table ( member_id int , provider_id varchar(10), claimDate date, CPT varchar(10), [State] char(2), Paid numeric ); insert into @claims values (123, 'abc', '2018-01-01', 'D11', 'NY', 10), (123, 'abc', '2018-01-01', 'D11', 'NY', 10), (123, 'abc', '2018-01-02', 'D11', 'NY', 5), (555, 'xyz', '2018-01-05', '01T', 'NJ', 39), (555, 'xyz', '2018-01-05', '01T', 'NJ', 39), (666, 'xyz', '2018-01-05', 'V93', 'NJ', 11); select [State], CPT, TheSum from ( select *, RowNum = row_number() over (partition by member_id, provider_id, claimDate, CPT order by member_id), TheSum = sum(Paid) over (partition by member_id, provider_id, claimDate, CPT order by member_id) from @claims ) x where x.RowNum = 1; 键上,但是您可以使用['name']访问数据:

例如:

['album']['images']

Demo

答案 1 :(得分:0)

如果我对它的理解正确,那么您想访问多个变暗的数组。 像这样尝试从$ result数组访问一个字段:

$ result [0] ['相册'] ['images'] [0] ['url']

这将返回“ https://i.scdn.co/image/ba052dff3d3cf0f5bd9b66a7ac707c2f4dfad065

$ result [0] ['相册'] ['images'] [1] ['url'] ==>“ https://i.scdn.co/image/20b02fb5aae2cfe790adb6b65433850a0977542c

$ result [0] ['相册'] ['images'] [2] ['url'] ==>“ https://i.scdn.co/image/668e5fb3a1044d4a2d7a90d3491612fe702d558b