Get first element in PHP stdObject

后端 未结 7 1150
日久生厌
日久生厌 2020-12-05 09:23

I have an object (stored as $videos) that looks like this

object(stdClass)#19 (3) {
  [0]=>
  object(stdClass)#20 (22) {
    [\"id\"]=>
    string(1) \         


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 09:48

    $videos->{0}->id worked for me.

    Since $videos and {0} both are objects, so we have to access id with $videos->{0}->id. The curly braces are required around 0, as omitting the braces will produce a syntax error : unexpected '0', expecting identifier or variable or '{' or '$'.

    I'm using PHP 5.4.3.

    In my case, neither $videos{0}->id and $videos{0}['id'] worked and shows error :

    Cannot use object of type stdClass as array.

提交回复
热议问题