循环遍历xml数组

时间:2014-04-29 06:29:55

标签: php xml

以下是我的代码,我想提取[field] => Array of index [1]的信息。这意味着[1] => 1030.670044. 这是xml文件,我想存储在我的数据库中,但无法在php中循环。 请帮助我。

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 1.0
        )

    [meta] => SimpleXMLElement Object
        (
            [type] => resource-list
        )

    [resources] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [start] => 0
                    [count] => 168
                )

            [resource] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [classname] => Quote
                                )

                            [field] => Array
                                (
                                    [0] => USD/KRW
                                    [1] => 1030.670044
                                    [2] => KRW=X
                                    [3] => 1398752590
                                    [4] => currency
                                    [5] => 2014-04-29T06:23:10+0000
                                    [6] => 0
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [classname] => Quote
                                )

                            [field] => Array
                                (
                                    [0] => SILVER 1 OZ 999 NY
                                    [1] => 0.051059
                                    [2] => XAG=X
                                    [3] => 1398721323
                                    [4] => currency
                                    [5] => 2014-04-28T21:42:03+0000
                                    [6] => 400
                                )

                        )

1 个答案:

答案 0 :(得分:2)

这是一个SimpleXML对象。如果变量名为$xml,您可以像这样访问字段[1]:

foreach ($xml->resources->resource as $res) {
   var_dump( $res->field[1] );
}