search
HomeBackend DevelopmentPHP Tutorial一个对象,怎么又同时是数组?

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是一个对象啊!怎么变成数组了?


回复讨论(解决方案)

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 方法返回的数组进行操作
你那样写也是可以的

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor