php json - 从Array中获取第一个元素

时间:2021-07-23 03:38:00

I'm writing code to show data from themoviedb.org API, but still failed to get correct results. I dont know what're wrongs to my PHP codes, but below code just give me one result from array, it should be 3 items. Below code are for testing only :

我正在编写代码来显示来自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++;
    }           
?>

But if i do print_r($data_movie), the data are available...

但如果我做print_r($ data_movie),数据可用......

The Json structure:

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
}

2 个解决方案

#1


2  

$i counter is useless:

$ i计数器没用:

$data = json_decode($result, true);
$data_movie = $data['results'];
foreach ($data_movie as $key => $val) {
    echo $val['poster_path'];
}

#2


0  

Your way of looping is strange, why aren't you just doing this:

你的循环方式很奇怪,你为什么不这样做:

<?php    
foreach ($data['results'] as $d) {
   print_r($d);
}           
?>

#1


2  

$i counter is useless:

$ i计数器没用:

$data = json_decode($result, true);
$data_movie = $data['results'];
foreach ($data_movie as $key => $val) {
    echo $val['poster_path'];
}

#2


0  

Your way of looping is strange, why aren't you just doing this:

你的循环方式很奇怪,你为什么不这样做:

<?php    
foreach ($data['results'] as $d) {
   print_r($d);
}           
?>