如何访问php花括号对象属性 [英] How to access php curly brace object property

查看:148
本文介绍了如何访问php花括号对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用名称(如整数)访问对象属性?

Possible Duplicate:
How to access object properties with names like integers?

print_r($myObj)给出以下结果:

stdClass Object
(
    [4021450] => stdClass Object
    (
        [property1] => ooo
        [property2] => xxx
    )
    [3971601] => stdClass Object
    (
        [property1] => 123
        [property2] => 356
    )
)

如何使用带有括号的大括号语法访问sub-object?

How can I using the curly brace syntax with variable to access the sub-object?

我尝试过:

$myObj->'3971601';                     // Parse error: syntax error  
$myObj->{'3971601'};                   // Works  
$id = 3971601; $myObj->{$id};          // Notice: Trying to get property of non-object  
$id = 3971601; $myObj->{''.$id};       // Notice: Trying to get property of non-object  
$arr = (array)$myObj; $arr[3971601];   // Notice: Undefined offset: 3971601
$arr = (array)$myObj; $arr['3971601']; // Notice: Undefined index: 3971601

推荐答案

您应该能够完全省略花括号:$myObj->$id.但是,您的最后4个示例表明有些不对劲.似乎在某些地方,$myObj被设置为null或其他一些非对象值.

You should be able to omit the curly braces entirely: $myObj->$id. Your last 4 examples, however, indicate something is amiss. It seems somewhere along the lines, $myObj was set null or to some other non-object value.

这篇关于如何访问php花括号对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆