一个对象,怎么又同时是数组?

php中文网
发布: 2016-06-23 14:08:50
原创
965人浏览过

simpleXML中,将xml文件转成了一个对象,根节点就是这个对象,访问子节点就是对象的成员属性。有下面这个xml文档:

<?xml version="1.0" encoding="utf-8"?><books><book lang="中文"><name>如何成功?</name><author>张三</author><price>39.8</price></book><book lang="English"><name>成功人士应该有的特质</name><author>李四</author><price>42.8</price></book></books>
登录后复制


$xml_obj=simplexml_load_file("books.xml"); $books=$xml_obj->book;$book=$books[0]; //这是第一本书,他是一个对象,他下面的子节点可以通过访问他的成员方法获取,但是他的属性,,,,,,$lang=$book["lang"];//获取他的属性,lang。以数组的方式访问,但是他不是一个对象吗?
登录后复制


我不明白的地方是这样的,访问节点属性的时候是以数组的方式去访问的,但是$book是一个对象啊!怎么变成数组了?

阿里云-虚拟数字人
阿里云-虚拟数字人

阿里云-虚拟数字人是什么? ...

阿里云-虚拟数字人 2
查看详情 阿里云-虚拟数字人

回复讨论(解决方案)

http://www.php.net/manual/zh/class.arrayaccess.php

    [book] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => 中文
                        )

                    [name] => 如何成功?
                    [author] => 张三
                    [price] => 39.8
                )

摘录手册一段,留意红色部分:
-------------------------------------------------------
The SimpleXMLElement class
(No version information available, might only be in SVN)
简介
Represents an element in an XML document. 
类摘要
SimpleXMLElement  implements Traversable {
……
-----------------------------------------------------------------

Traversable 是SPL的一个类(目前手册并无具体介绍,但去 php.net 能发掘到)
再摘录一小段给你

-----------------------------------------------------------------------------

Traversable Interface Reference
[Zend engine classes] 
Interface to detect a class is traversable using foreach. More...

Detailed Description
Interface to detect a class is traversable using foreach.  //这就意味着是可迭代对象

Since:
PHP 5.0
Abstract base interface that cannot be implemented alone. Instead it must be implemented by either IteratorAggregate or Iterator.

Note:
Internal classes that implement this interface can be used in a foreach construct and do not need to implement IteratorAggregate or Iterator.
This is an engine internal interface which cannot be implemented in PHP scripts. Either IteratorAggregate or Iterator must be used instead. 


Definition at line 509 of file spl.php.


--------------------------------------------------------------------------------
The documentation for this interface was generated from the following file:
spl.php

    [book] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => 中文
                        )

                    [name] => 如何成功?
                    [author] => 张三
                    [price] => 39.8
                )


  

[@attributes] => Array
登录后复制


这里的@是什么意思啊?我对正宗的数组var_dump没有出现这个@符号。

SimpleXMLElement 对象的属性数组
一般用遍历 attributes 方法返回的数组进行操作
你那样写也是可以的

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号