通过xml数组循环


Loop through xml array

下面是我的代码,我想提取[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
                                )
                        )

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

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