【问题标题】:How to loop through an array of SimpleXMLElement objects and return node default value?如何遍历 SimpleXMLElement 对象数组并返回节点默认值?
【发布时间】:2014-03-10 01:03:17
【问题描述】:

我正在尝试获取每个字段的默认值,但我不知道如何循环遍历所有对象。尝试使用 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
                                        )
                                )
                        )
                )
)

【问题讨论】:

    标签: php arrays simplexml


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2012-03-08
      • 2020-07-21
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多