在PHP中从JSON中提取值

时间:2015-09-20 12:20:39

标签: php json discogs-api

我目前正在使用PHP试验Discogs API。我对其API的查询以JSON形式返回,我可以使用以下方法解码:

$trackMeta = json_decode($trackMeta, true);

然后,我希望能够访问返回的数据中的某些元素。使用print_r($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 ) ) ) 

尝试在$trackMeta上使用foreach循环时,我只能返回171,我认为这与per_page页和page有关。

如何在此阵列中更深入地访问数据?例如拇指或年份或流派等值?

1 个答案:

答案 0 :(得分:0)

$trackMeta是一个关联数组:

$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
        )
    )
);

例如,您可以使用以下代码访问thumb

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