Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rubol's avatar
Level 2

Cannot use object of type stdClass as array

I'm using Instagram Package for laravel. The below method returns results in array form:

public static function instaPosts() { $instagram = new Instagram(); $items = $instagram->get('lauraiz');

return $items; }

array:20 [▼
0 => {#430 ▼
    +"id": "1554751710723134586_11443906"
    +"code": "BWTlnGHlVR6"
    +"user": {#433 ▶}
    +"images": {#427 ▶}
    +"created_time": "1499560868"
    +"caption": {#432 ▶}
    +"likes": {#440 ▶}
    +"comments": {#449 ▶}
    +"can_view_comments": true
+"can_delete_comments": false
    +"type": "carousel"
+"link": "https://www.instagram.com/p/BWTlnGHlVR6/"
    +"location": null
    +"alt_media_url": null
    +"carousel_media": array:4 [▶]
}
1 => {#482 ▶}
 2 => {#504 ▶}
3 => {#524 ▶}
  4 => {#546 ▶}
  5 => {#569 ▶}
  6 => {#591 ▶}
  7 => {#613 ▶}
  8 => {#633 ▶}
  9 => {#656 ▶}
  10 => {#679 ▶}
  11 => {#701 ▶}
  12 => {#724 ▶}
  13 => {#744 ▶}
  14 => {#798 ▶}
  15 => {#822 ▶}
  16 => {#844 ▶}
  17 => {#866 ▶}
  18 => {#888 ▶}
  19 => {#910 ▶}
]

I want to get url of thumbnail from images key of every array index. I've tried the following code:

array:20 [▼
0 => {#430 ▼
    +"id": "1554751710723134586_11443906"
    +"code": "BWTlnGHlVR6"
    +"user": {#433 ▶}
    +"images": {#427 ▼
      +"thumbnail": {#428 ▼
        +"width": 150
        +"height": 150
        +"url": "https://instagram.fktm3-1.fna.fbcdn.net/t51.2885-      15/s150x150/e35/19954999_1933558183580105_9022206750941511680_n.jpg"
  }
         +"low_resolution": {#439 ▶}
    +"standard_resolution": {#429 ▶}
    }
    +"created_time": "1499560868"
    +"caption": {#432 ▶}
    +"likes": {#440 ▶}
    +"comments": {#449 ▶}
    +"can_view_comments": true
    +"can_delete_comments": false
    +"type": "carousel"
    +"link": "https://www.instagram.com/p/BWTlnGHlVR6/"
    +"location": null
    +"alt_media_url": null
    +"carousel_media": array:4 [▶]
  }

And my code in view:

                        @foreach($items as $item)
                        @foreach($item['images'] as $k)                             
                        <li><img src="{{$k['thumbnail']['url']}}" alt="instagram"></li>
                        @endforeach
                        @endforeach 

But I'm getting error of:

Cannot use object of type stdClass as array

0 likes
1 reply
mikevrind's avatar
Level 7

The error says everything you need to know.

$item in your second foreach is an object, not an array and your are treating it like it's an array.

$k->thumbnail 

But if I look at the data dump, I don't think you need the second foreach.

$item->images->thumbnail->url

Please or to participate in this conversation.