简体   繁体   English

无法将类型…的对象用作数组

[英]Cannot use object of type … as array

I am using an API from themoviedb.org, which is returning an array: 我正在使用来自themoviedb.org的API,该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 ▶}
]

I am trying access it like I normally would an array: 我试图像通常使用数组那样访问它:

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

But I am getting this error: 但我收到此错误:

"Cannot use object of type TVShow as array" “不能将TVShow类型的对象用作数组”

Could someone please help? 有人可以帮忙吗? Thanks! 谢谢!

I solved my own question with lot's of handholding from @ArtisticPhoenix . 我通过@ArtisticPhoenix的大量解决方法解决了自己的问题。

I am using TMDB PHP API wrapper. 我正在使用TMDB PHP API包装器。 In order to access the "original_name" value, I needed to do this: 为了访问“ original_name”值,我需要这样做:

echo $item->getName();

As detailed in the wrapper documentation . 如包装文档中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM