不能使用类型的对象...作为数组

时间:2017-10-06 02:04:46

标签: php arrays object

我正在使用来自themoviedb.org的API,它返回一个数组:

array:20 [▼
  0 => TVShow {#186 ▼
    -_data: array:13 [▼
      "original_name" => "Star Trek: Discovery"
      "id" => 67198
      "name" => "Star Trek: Discovery"
      "vote_count" => 48
      "vote_average" => 6.3
      "poster_path" => "/ihvG9dCEnVU3gmMUftTkRICNdJf.jpg"
      "first_air_date" => "2017-09-24"
      "popularity" => 75.562899
      "genre_ids" => array:1 [▶]
      "original_language" => "en"
      "backdrop_path" => "/s3kVP6R3LbJvvoPnDQEcJNEH2d0.jpg"
      "overview" => "Ten years before Kirk, Spock, and the Enterprise there was Discovery.  Star Trek, one of the most iconic and influential global television franchises, returns 5 ▶"
      "origin_country" => array:1 [▶]
    ]
  }
  1 => TVShow {#187 ▶}
  2 => TVShow {#193 ▶}
  3 => TVShow {#194 ▶}
  4 => TVShow {#195 ▶}
  5 => TVShow {#196 ▶}
]

我正在尝试访问它,就像我通常会使用数组一样:

foreach($array as $item {
  echo $item["original_name"];
}

但是我收到了这个错误:

  

"不能使用TVShow类型的对象作为数组"

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我用很多来自 @ArtisticPhoenix 的手段解决了我自己的问题。

我正在使用TMDB PHP API包装器。为了访问“original_name”值,我需要这样做:

echo $item->getName();

详见封送器documentation