简体   繁体   中英

Cannot use object of type … as array

I am using an API from themoviedb.org, which is returning an array:

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"

Could someone please help? Thanks!

I solved my own question with lot's of handholding from @ArtisticPhoenix .

I am using TMDB PHP API wrapper. In order to access the "original_name" value, I needed to do this:

echo $item->getName();

As detailed in the wrapper documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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