PHP Error Cannot use object of type stdClass as array in Error Resolution

  • 2021-07-01 06:46:02
  • OfStack

Many people appear "Fatal error: Cannot use object of type stdClass as array in …" when PHP outputs a 2-dimensional array.
The solution analysis is as follows:

There is one 2-dimensional array:

Array ( 
 [0] => stdClass Object ( 
  [id] => 1 
  [title] =>  Recruitment information  
  [size] => 300*150 
  [pic] => ./upload/20140602093535.jpg 
  [state] => 0 ) 
 [1] => stdClass Object ( 
  [id] => 2 
  [title] =>  Home page headline  
  [size] => 300*150 
  [pic] => ./upload/20140602093443.jpg 
  [state] => 0 ) 
 )

The output starts writing by: $pic [0] [title]

As a result, the above error appears.

In fact, the array is returned is 1 object, can not be displayed directly with [], the correct output method is: $pic [0]- > title

Problem solved!


Related articles: