我正在编写代码以显示来自themoviedb.org API的数据,但仍无法获得正确的结果。我不知道我的PHP代码有什么错误,但下面的代码只给我一个数组结果,它应该是3个项目。 以下代码仅供测试:
<?php
...
$data = json_decode($result, true);
$data_movie = array($data['results']);
$i = 0;
foreach ($data_movie as $key => $val)
{
echo $val[$i]['poster_path']."<br/>";
$i++;
}
?>
但如果我print_r($data_movie)
,则数据可用......
Json 结构:
{
"page": 1,
"results": [
{
"poster_path": "/6bCplVkhowCjTHXWv49UjRPn0eK.jpg",
"adult": false,
"overview": "Fearing the actions of a god-like Super Hero left unchecked, Gotham City’s own formidable, forceful vigilante takes on Metropolis’s most revered, modern-day savior, while the world wrestles with what sort of hero it really needs. And with Batman and Superman at war with one another, a new threat quickly arises, putting mankind in greater danger than it’s ever known before.",
"release_date": "2016-03-23",
"genre_ids": [
28,
12,
14,
878
],
"id": 209112,
"original_title": "Batman v Superman: Dawn of Justice",
"original_language": "en",
"title": "Batman v Superman: Dawn of Justice",
"backdrop_path": "/15PbZtjRJ4zgQA8XS0otL70piQi.jpg",
"popularity": 7.969661,
"vote_count": 42,
"video": false,
"vote_average": 5.68
},
{
"poster_path": "/tSFBh9Ayn5uiwbUK9HvD2lrRgaQ.jpg",
"adult": false,
"overview": "Beatrice Prior and Tobias Eaton venture into the world outside of the fence and are taken into protective custody by a mysterious agency known as the Bureau of Genetic Welfare.",
"release_date": "2016-03-09",
"genre_ids": [
12,
878
],
"id": 262504,
"original_title": "The Divergent Series: Allegiant",
"original_language": "en",
"title": "The Divergent Series: Allegiant",
"backdrop_path": "/jaoDlr7XGGtHs1VpN9macrbXheO.jpg",
"popularity": 4.624446,
"vote_count": 42,
"video": false,
"vote_average": 6.81
},
{
"poster_path": "/d936U33f5n6bRtzDHh1gmDdriKr.jpg",
"adult": false,
"overview": "Waking up from a car accident, a young woman finds herself in the basement of a man who says he's saved her life from a chemical attack that has left the outside uninhabitable.",
"release_date": "2016-03-09",
"genre_ids": [
18,
9648,
878,
53
],
"id": 333371,
"original_title": "10 Cloverfield Lane",
"original_language": "en",
"title": "10 Cloverfield Lane",
"backdrop_path": "/u7GyYxTNx8LDYpma605YZvjdOVp.jpg",
"popularity": 2.90869,
"vote_count": 10,
"video": false,
"vote_average": 2.95
}
],
"dates": {
"maximum": "2016-03-27",
"minimum": "2016-03-06"
},
"total_pages": 5,
"total_results": 97
}
答案 0 :(得分:2)
$i
计数器无用:
$data = json_decode($result, true);
$data_movie = $data['results'];
foreach ($data_movie as $key => $val) {
echo $val['poster_path'];
}
答案 1 :(得分:0)
你的循环方式很奇怪,为什么你不这样做:
<?php
foreach ($data['results'] as $d) {
print_r($d);
}
?>