隔离php对象变量

时间:2011-06-17 19:00:30

标签: php xml variables object

我将一些XML作为对象返回,如下所示:

SwitchvoxResponse Object
(
[apiStatus:private] => success
[apiErrors:private] => Array
    (
    )

[apiResult:private] => Array
    (
        [calls] => Array
            (
                [page_number] => 1
                [total_pages] => 1
                [items_per_page] => 50
                [total_items] => 1
                [call] => Array
                    (
                        [0] => Array
                            (
                                [id] => 14301
                                [origination] => outgoing
                                [start_time] => 2011-06-17 13:40:58
                                [from] => CALLER_NAME <4485>
                                [from_account_id] => 1120
                                [from_name] => CALLER_NAME
                                [from_number] => 4485
                                [to] => CALLEE_NAME <6534>
                                [to_account_id] => 1101
                                [to_name] => CALLEE_NAME
                                [to_number] => 6534
                                [total_duration] => 47
                                [talk_duration] => 43
                                [events] => Array
                                    (
                                        [event] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [start_time] => 2011-06-17 13:40:58
                                                        [type] => OUTGOING
                                                        [display] => Dialed number (6534)
                                                    )

                                                [1] => Array
                                                    (
                                                        [start_time] => 2011-06-17 13:40:58
                                                        [type] => INTERNAL
                                                        [display] => Rang CALLEE_NAME <6534>
                                                    )

                                                [2] => Array
                                                    (
                                                        [start_time] => 2011-06-17 13:41:02
                                                        [type] => TALKING
                                                        [display] => Talked to CALLEE_NAME <6534> for 43 seconds
                                                    )

                                                [3] => Array
                                                    (
                                                        [start_time] => 2011-06-17 13:41:45
                                                        [type] => HANGUP
                                                        [display] => Call was hung up by CALLER_NAME <4485>
                                                    )

                                            )

                                    )

                            )
                    )
            )
    )
)

如何提取这些变量的值?

2 个答案:

答案 0 :(得分:1)

可以通过SwitchvoxResponse::getResult()方法访问结果。鉴于$object是问题中引用的SwitchvoxResponse对象,下面的示例会循环遍历每个call并打印from值。

$result = $object->getResult();
foreach ($result['calls']['call'] as $call) {
    echo $call['from'];
}

同样,响应状态通过$object->getResponseStatus()获取,任何错误都来自$object->getErrors()

回复状态可以是SV_RESPONSE_SUCCESSSV_RESPONSE_FAULTSV_RESPONSE_FAILED之一。


编辑重新。评论

要获取第一个电话的项目,只需执行以下操作:

$result = $object->getResult();
$call = $result['calls']['call'][0];
// And access the values like
echo $call['from_name']; 

答案 1 :(得分:0)

所有三个属性都标记为private,因此必须通过标记为public

的访问方法获取它们