How to access a member of an stdClass in PHP that starts with an @

前端 未结 4 528
不知归路
不知归路 2021-01-04 09:10

I have some json object that I decoded, and one of the attributes starts with an \"@\" and I can\'t access the element with php because it throws an error.

          


        
4条回答
  •  自闭症患者
    2021-01-04 09:30

    You can access it by a string:

    echo $obj->{'@attributes'}->id; // levaka0B8a
    

    Or a variable:

    $name = '@attributes';
    echo $obj->$name->id;
    

    For more information on how variables are defined and used, see the following docs:

    • Variable Basics - Useful for learning what can be accessed as a variable without needing to use strings.
    • Variable Variables - How we used the variable to act as the name for another variable. This can be dangerous so tread carefully

提交回复
热议问题