Get first element in PHP stdObject

后端 未结 7 1130
日久生厌
日久生厌 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:03

    Playing with Php interactive shell, Php 7:

    ➜  ~ php -a
    Interactive shell
    
    php > $v = (object) ["toto" => "hello"];
    php > var_dump($v);
    object(stdClass)#1 (1) {
      ["toto"]=>
      string(5) "hello"
    }
    php > echo $v{0};
    PHP Warning:  Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
    Stack trace:
    #0 {main}
      thrown in php shell code on line 1
    
    Warning: Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
    Stack trace:
    #0 {main}
      thrown in php shell code on line 1
    php > echo $v->{0};
    PHP Notice:  Undefined property: stdClass::$0 in php shell code on line 1
    
    Notice: Undefined property: stdClass::$0 in php shell code on line 1
    php > echo current($v);
    hello
    

    Only current is working with object.

提交回复
热议问题