为什么我的php函数不能从JSON数组中获取数据?

时间:2016-01-21 21:18:33

标签: php arrays json

我有一个页面,在TMDB的帮助下,将TMDB-API Wrapper的电视节目信息作为json数组提取。我试图让页面显示电视节目的['网络']信息。

我已经为此编写了一个循环通过网络阵列但我无法使其工作的函数。谁能告诉我我做错了什么?感谢。

这是我的功能不起作用。

//Get networks
   public function getNetworks() {
        $networks = array();

        foreach($this->_data['networks'] as $data){
            $networks[] = new Network($data, $this->getName());
        }

        return $networks;
    }

Display.php的

//Call the function and loop though results
 echo '  <li>Networks: <ul>';
            $networks = $tvShow->getNetworks();
            foreach($networks as $network){
                echo '<li>'. $network->getID() .'</li>';
            }

这确实有效(写在包装器中而不是我)

 public function getSeasons() {
        $seasons = array();

        foreach($this->_data['seasons'] as $data){
            $seasons[] = new Season($data, $this->getID());
        }
    return $seasons;
}

这确实有效

//Get Network
public function getNetworks() {
        return $this->_data['networks'][0]['name'];
}

然后在display.php

echo '<b>'. $tvShow->getNetworks() .'</b>';

这是json数组

{
    "backdrop_path": "/c8A5IYqIT1ez15sqA8wX5tCDTmF.jpg",
    "created_by": [
        {
            "id": 1187819,
            "name": "Matthew Weiner",
            "profile_path": null
        }
    ],
    "episode_run_time": [
        47,
        45
    ],
    "first_air_date": "2007-07-19",
    "genres": [
        {
            "id": 18,
            "name": "Drama"
        }
    ],
    "homepage": "http://www.amc.com/shows/mad-men",
    "id": 1104,
    "in_production": false,
    "languages": [
        "en"
    ],
    "last_air_date": "2015-05-17",
    "name": "Mad Men",
    "networks": [
        {
            "id": 174,
            "name": "AMC"
        }
    ],

Tvshow Class

class TVShow{

    //------------------------------------------------------------------------------
    // Class Variables
    //------------------------------------------------------------------------------

    private $_data;

    /**
     *  Construct Class
     *
     *  @param array $data An array with the data of the TVShow
     */
    public function __construct($data) {
        $this->_data = $data;
    }

0 个答案:

没有答案