【问题标题】:echo out values from a php object [closed]从 php 对象中回显值 [关闭]
【发布时间】:2012-09-23 03:39:56
【问题描述】:

我有一个名为$result的php对象(来自json_decode),下面是该对象的print_r。我试图呼应问题编号和答案。例如:

4 answer1
5 answer2

我该怎么做。

stdClass Object
    (
    [request] => stdClass Object        (
            [other] => stdClass Object
                (
                    [4] => stdClass Object
                        (
                            [answer] => answer1
                        )
                    [5] => stdClass Object
                        (
                            [answer] => answer2
                        )
                )
        )
    )

我还在学习 OOP。作为一个简单的方法,我尝试echo $result->request->other->4->answer 回应第一个答案,但这似乎不对。感谢您的帮助。

【问题讨论】:

  • 伙计,这是一个 RTFM 问题 - 你试过 - 阅读 php.net/manual/en/book.array.php 甚至 php.net/manual/en/language.oop5.php 怎么样
  • @AdrianCornish 这个问题的真正问题是how to get the value of the number property of an object,你的两个链接没有多大帮助。
  • 所以问题是错误的并且质量极低,因为 OP 不知道他们在做什么或想要什么。这永远不能帮助别人

标签: php object


【解决方案1】:

你需要用{}包裹数字属性:

echo $result->request->other->{4}->answer;

【讨论】:

  • @AdrianCornish 是的,这是对这个问题的直接回答。 adding a method to the class 是什么意思?上哪个班?图片这是json_decode的结果。
  • @AdrianCornish 这只是一个例子,重点是 stdClass 不是你创建的。
  • @AdrianCornish 他很接近,唯一的问题是属性名称是数字。你怎么能说unspecified call?你能告诉你如何访问answer 的值吗?
  • @xdazz,是的,这是 json 解码的。谢谢,您的回答有效。我只需要循环通过它。非常感谢。
  • @AdrianCornish 这不是幸运的猜测,如果你知道 php,几乎是 90%。你错过了即使你包装响应对象给它一些方法,你仍然需要访问原始对象。
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多