【问题标题】:Getting array key(protected)-value from an array inside object (response from RTM-php)?从对象内部的数组中获取数组键(受保护)值(来自 RTM-php 的响应)?
【发布时间】:2014-07-11 06:07:51
【问题描述】:

当为 RTM (https://github.com/bartosz-maciaszek/php-rtm) 使用 php 库时,我收到了一个特定任务列表的响应,如下所示:

Rtm\DataContainer Object
(
    [attributes:Rtm\DataContainer:private] => Array
        (
            [0] => Rtm\DataContainer Object
                (
                    [attributes:Rtm\DataContainer:private] => Array
                        (
                            [id] => 19594773
                            [taskseries] => Rtm\DataContainer Object
                                (
                                    [attributes:Rtm\DataContainer:private] => Array
                                        (
                                            [id] => 310899576
                                            [created] => 2013-10-03T05:35:52Z
                                            [modified] => 2013-11-06T17:24:36Z
                                            [name] => A new task
                                            [source] => js
                                            [url] => 
                                            [location_id] =>

                                        )
                                )
                        )
              )
      )
)

我想获取[name] 的值。我该怎么做?

【问题讨论】:

    标签: php arrays object protected


    【解决方案1】:

    据我从文档中了解到,您必须调用类似

    $receivedObject->getTaskSeries()->getName()
    

    或者有一个建议将 toArray 或 toJson 应用于返回的对象 - 这应该可以。

    【讨论】:

    • 感谢您的回答,但getName() 给出了致命错误:Call to a member function getName() on a non-object。 toArray 可能是正确的方法。
    • 好的。把它修好了..使用 foreach 遍历它对我有用。但是对于真正的响应对象,我必须使用 foreach 两次:foreach ($response as $item) { $ts = $item->getTaskseries(); foreach ($ts as $item2) { $name[] = $item2->getName(); } } print_r($name[0]);
    猜你喜欢
    • 2017-03-19
    • 2018-03-12
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2014-10-15
    • 2018-07-19
    • 2017-06-03
    • 2021-08-22
    相关资源
    最近更新 更多