如何遍历XML数据?

时间:2017-12-29 作者:user3149814

我连接了一个api,得到了一个xml格式的响应,并将其转换为数组,但现在我仍在研究如何遍历这个数组。例如,我想从这个数组中获取所有注释。这是我的阵列

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => array
        )

    [survey] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [censored] => false
                    [comments] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [nil] => true
                                )

                        )

                    [completed-at] => 2017-12-29T08:47:08-05:00
                    [flagged-for-follow-up] => false
                    [has-notes] => false
                    [invited-at] => 2017-12-28T18:52:40-05:00
                    [net-promoter-label] => promoter
                    [public-response] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [nil] => true
                                )

                        )

                    [recommendation-likelihood] => 10
                    [updated-at] => 2017-12-29T08:47:08-05:00
                    [customer-reference] => 553
                    [customer-full-name] => Bill Hoeninger
                    [public-reviewer-name] => B.H.
                    [notes] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => array
                                )

                        )

                )
            [1] => SimpleXMLElement Object
                (
                    [censored] => false
                    [comments] => Friendly, attention to scheduling,, effective coaching
                    [completed-at] => 2017-12-29T05:26:03-05:00
                    [flagged-for-follow-up] => false
                    [has-notes] => false
                    [invited-at] => 2017-12-28T18:56:18-05:00
                    [net-promoter-label] => promoter
                    [public-response] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [nil] => true
                                )

                        )

                    [recommendation-likelihood] => 10
                    [updated-at] => 2017-12-29T05:26:29-05:00
                    [job-reference] => 19855
                    [customer-full-name] => Javaid Yousuf
                    [public-reviewer-name] => J.Y.
                    [notes] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => array
                                )

                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [censored] => false
                    [comments] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [nil] => true
                                )

                        )

                    [completed-at] => 2017-12-29T00:39:06-05:00
                    [flagged-for-follow-up] => false
                    [has-notes] => false
                    [invited-at] => 2017-12-28T18:54:58-05:00
                    [net-promoter-label] => promoter
                    [public-response] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [nil] => true
                                )

                        )

                    [recommendation-likelihood] => 10
                    [updated-at] => 2017-12-29T00:39:06-05:00
                    [job-reference] => 21505
                    [customer-full-name] => Julia Taylor
                    [public-reviewer-name] => J.T.
                    [notes] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => array
                                )

                        )

                )

        )

)
请帮帮我我被困在这里了

非常感谢。

1 个回复
SO网友:majick

看起来需要循环XML对象结果,例如:

$comments = array();
foreach ($object->survey as $index => $survey) {
    if (is_string($survey->comments)) {$comments[$index] = $survey->comments;}
}
print_r($comments); // to see comments

结束

相关推荐

是否将功能从unctions.php移至类?

这个functions.php 我正在处理的主题中的文件变得太大,难以管理,所以我正在考虑将一些功能移动到类中。这是个坏主意吗?不知道怎么做:我需要让他们成为单身吗?我只是在函数中调用新的MyClass吗。php?拆分功能的最佳实践是什么(目前我是do it be部分,例如标题功能、内容功能等)