如何遍历SimpleXMLElement对象数组并返回节点默认值?

时间:2014-02-11 23:32:40

标签: php arrays simplexml

我试图获取每个字段的默认值,但我无法弄清楚如何遍历所有对象。尝试将它们转换为带有json_decode的简单数组,但它不清楚循环的内容。

这是:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => params
        )

    [fieldset] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Cat1
                        )

                    [field] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => 
                                    [type] => list
                                    [default] => 1
                                )
                        )
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Item2
                        )

                    [field] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post1
                                            [type] => text
                                            [default] => 5
                                        )
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post2
                                            [type] => text
                                            [default] => 18
                                        )
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post3
                                            [type] => text
                                            [default] => 15
                                        )

                                    [option] => Array
                                        (
                                            [0] => Blue
                                            [1] => Green
                                        )
                                )
                        )
                )

                      [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Cat2
                        )

                    [field] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post6
                                            [type] => text
                                            [default] => 3
                                        )
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post7
                                            [type] => text
                                            [default] => 36
                                        )
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => post7
                                            [type] => text
                                            [default] => 88
                                        )
                                )
                        )
                )
)

1 个答案:

答案 0 :(得分:1)

试试这个($xml是你的根对象)

foreach($xml->fieldset as $fieldset) {
    foreach($fieldset->field as $field) echo (string)$field['default'];
}