dongnuo2879 2014-02-11 23:32
浏览 50
已采纳

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

I am trying to get default value for each field but I just can't figure out how to loop through all the objects. Tried with converting them to a simple array with json_decode but it just not clear as to what to loop.

Here it is:

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条回答 默认 最新

  • dongqu9917 2014-02-11 23:49
    关注

    Try this (where $xml is your root object)

    foreach($xml->fieldset as $fieldset) {
        foreach($fieldset->field as $field) echo (string)$field['default'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?