【问题标题】:Trying to print properties of stdClass object in Php starting with literal '$'试图在 PHP 中打印 stdClass 对象的属性,以文字“$”开头
【发布时间】:2013-04-05 22:43:56
【问题描述】:

我的对象名称是 $distinct1

print_r($distinct1) 显示了下面的对象,但是当我尝试这样做时

echo $distinct1->properties->city

echo $distinct1->distinct_id

我没有得到任何价值回报。知道发生了什么吗?

stdClass Object
(
    [$distinct_id] => AAA
    [$properties] => stdClass Object
        (
            [$city] => Palo Alto
            [$country_code] => US
            [$region] => California
            [$name] => John Smith
)
)

【问题讨论】:

  • 呃,你的字段是 'properties' 还是 '$properties'

标签: php echo stdclass


【解决方案1】:

嗯,如果你的属性真的是这样命名的:

$distinct1->{'$properties'}->{'$city'}

但如果可能的话,我会研究一下设置它的机制并在那里修复它。

【讨论】:

    【解决方案2】:

    出于某种奇怪的原因,该对象的属性名称实际上以 $ 开头。

    echo $distinct1->{'$properties'}->{'$city'};
    

    【讨论】:

    • 谢谢。这行得通。我从第三方 API 提取数据,不知道他们为什么在属性名称前使用 $。
    猜你喜欢
    • 2011-12-07
    • 2012-01-19
    • 2016-02-06
    • 2017-04-23
    • 2020-01-02
    • 2013-10-17
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多