Get first element in PHP stdObject

后端 未结 7 1128
日久生厌
日久生厌 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 10:07

    Update 2019

    Moving on to the best practices of OOPS, @MrTrick's answer must be marked as correct, although my answer provides a hacked solution its not the best method.

    Simply iterate its using {}

    Example:

    $videos{0}->id
    

    This way your object is not destroyed and you can easily iterate through object.

    For PHP 5.6 and below use this

    $videos{0}['id']
    

提交回复
热议问题