开发者

Help Decoding JSON

开发者 https://www.devze.com 2023-03-17 09:37 出处:网络
I\'m trying to get the \"screenshotUrls\" string from this piece of json: $request_url = \'http://itunes.apple.com/search?term=ibooks&country=us&entity=software&limit=1\';

I'm trying to get the "screenshotUrls" string from this piece of json:

$request_url = 'http://itunes.apple.com/search?term=ibooks&country=us&entity=software&limit=1';
$json = file_get_contents($request_url);
$decode 开发者_如何转开发= json_decode($json, true);

echo $decode['results'][0]['screenshotUrls'];

But I get only text "Array"

What have I done wrong?


Try

var_dump($decode['results'][0]['screenshotUrls']);

IF you get 'Array' output by PHP that means that you're trying to echo an actual array (or the string 'Array'...). That means you need to get a specific index value.


Since $decode['results']['0']['screenshotUrls'] is an array, if you want just a string (say, delimited by commas), you could use

echo implode(",", $decode['results']['0']['screenshotUrls']);

This will iterate over the array, and return a comma-separated string of all the URLs.


Do a var_dump($decode['results']['0']['screenshotUrls']). You'll find that the ['screenshotUrls'] is actually an Array, containing one or more URLs (hence the plural 'urls' in its name).


There's nothing wrong with your code so far, but $decode['results'][0]['screenshotUrls'] is an array of all the URLs to screenshots. To go through each one individualy, you need to do:

forearch ($decode['results'][0]['screenshotUrls'] as $url) {
    // Do stuff here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号