douying1119 2010-05-05 21:46
浏览 21
已采纳

简单的XML变量返回“0”

Ok, so im working with tumblr's API and im using SimpleXMLElement with php and ive run into a problem.

Basically, when I call on a variable it just prints a zero.

for instance :

<?php echo $data->posts->post[0]->photo-url[5]; ?>

This just breaks the page, and when I leave out the [5] it just prints zero

I want to echo the photo-url with the number [5] but I cant get my head around this and its killing me. I just dont understand why it wont work.

Heres a sample of the simpleXMLElement print-out :

SimpleXMLElement Object

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

[posts] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => photo
            )

        [post] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [id] => 574166180
                                [url] => http://young-n-reckless.tumblr.com/post/574166180
                                [url-with-slug] => http://young-n-reckless.tumblr.com/post/574166180
                                [type] => photo
                                [date-gmt] => 2010-05-05 19:52:46 GMT
                                [date] => Wed, 05 May 2010 15:52:46
                                [unix-timestamp] => 1273089166
                                [format] => html
                                [reblog-key] => vc9EfT7d
                                [slug] => 
                                [tumblelog] => young-n-reckless
                                [bookmarklet] => true
                            )

                        [tumblelog] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [title] => Young & Reckless
                                        [name] => young-n-reckless
                                        [url] => http://young-n-reckless.tumblr.com/
                                        [timezone] => US/Eastern
                                        [avatar-url-16] => http://24.media.tumblr.com/avatar_424f776dd55c_16.png
                                        [avatar-url-24] => http://24.media.tumblr.com/avatar_424f776dd55c_24.png
                                        [avatar-url-30] => http://24.media.tumblr.com/avatar_424f776dd55c_30.png
                                        [avatar-url-40] => http://25.media.tumblr.com/avatar_424f776dd55c_40.png
                                        [avatar-url-48] => http://24.media.tumblr.com/avatar_424f776dd55c_48.png
                                        [avatar-url-64] => http://26.media.tumblr.com/avatar_424f776dd55c_64.png
                                        [avatar-url-96] => http://25.media.tumblr.com/avatar_424f776dd55c_96.png
                                        [avatar-url-128] => http://24.media.tumblr.com/avatar_424f776dd55c_128.png
                                        [avatar-url-512] => http://29.media.tumblr.com/avatar_424f776dd55c_512.png
                                    )

                            )

                        [photo-link-url] => http://www.flickr.com/photos/katiemabey/3429588812/
                        [photo-url] => Array
                            (
                                [0] => http://30.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_500.jpg
                                [1] => http://30.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_500.jpg
                                [2] => http://25.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_400.jpg
                                [3] => http://28.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_250.jpg
                                [4] => http://30.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_100.jpg
                                [5] => http://30.media.tumblr.com/tumblr_l1w1qd7Zcg1qatgoto1_75sq.jpg
                            )

                    )

Any help would be greatly appreciated, and thanks in advance. Warren

  • 写回答

1条回答 默认 最新

  • douzhi6160 2010-05-05 21:54
    关注

    If the element name has a dash in it, you need to wrap it like so:

    <?php echo $data->posts->post[0]->{'photo-url'}[5]; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题