douyanqu9722 2015-09-20 04:20
浏览 30
已采纳

在PHP中从JSON中提取值

I am currently experimenting with the Discogs API, using PHP. My query against their API is returned as JSON, which I can decoding using:

$trackMeta = json_decode($trackMeta, true);

I'd then like to be able to access certain elements within the data that is returned. Using print_r($trackMeta);outputs the below data:

Array ( [pagination] => Array ( [per_page] => 1 [pages] => 7 [page] => 1 [urls] => Array ( [last] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7 [next] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2 ) [items] => 7 ) [results] => Array ( [0] => Array ( [style] => Array ( [0] => Drum n Bass ) [thumb] => http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg [format] => Array ( [0] => Vinyl [1] => 12" [2] => 45 RPM ) [country] => UK [barcode] => Array ( ) [uri] => /Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189 [community] => Array ( [want] => 66 [have] => 81 ) [label] => Array ( [0] => Siren Records ) [catno] => SIREN001 [year] => 2012 [genre] => Array ( [0] => Electronic ) [title] => Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude [resource_url] => http://api.discogs.com/masters/433189 [type] => master [id] => 433189 ) ) ) 

When trying to use a foreach loop on $trackMeta, I can only return 171 which I believe relates to per_page pages and page.

How can I access data deeper in this array? For example values such as thumb or year or genre?

  • 写回答

1条回答 默认 最新

  • douyin2435 2015-09-20 05:45
    关注

    $trackMeta is an associative array:

    $trackMeta = array (
        'pagination' => array (
            'per_page' => 1,
            'pages' => 7,
            'page' => 1,
            'urls' => array (
                'last' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7',
                'next' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2'
            ),
            'items' => 7
        ),
        'results' => array (
            0 => array (
                'style' => array (
                    0 => 'Drum n Bass'
                ),
                'thumb' => 'http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg',
                'format' => array (
                    0 => 'Vinyl',
                    1 => '12"',
                    2 => '45 RPM'
                ),
                'country' => 'UK',
                'barcode' => array ( ),
                'uri' => '/Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189',
                'community' => array (
                    'want' => 66,
                    'have' => 81
                ),
                'label' => array (
                    0 => 'Siren Records'
                ),
                'catno' => 'SIREN001',
                'year' => 2012,
                'genre' => array (
                    0 => 'Electronic'
                ),
                'title' => 'Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude',
                'resource_url' => 'http://api.discogs.com/masters/433189',
                'type' => 'master',
                'id' => 433189
            )
        )
    );
    

    For example, you can access thumb with the following code:

    print_r($trackMeta['results'][0]['thumb']);

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部