Home > php教程 > php手册 > body text

PHP中simplexml_load_string函数使用说明

WBOY
Release: 2016-06-13 12:12:18
Original
784 people have browsed it

先用一段代码重现一下问题
乍一看,结果很让人费解:

复制代码 代码如下:


$string =
hello
world

EOF;

$data = simplexml_load_string($string);

print_r($data);
print_r($data->foo);
?>


乍一看,结果很让人费解:

复制代码 代码如下:


SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)


明明print_r显示foo是一个有两个bar元素的数组,但是最后却仅仅显示了一个bar元素!
原因其实很简单,在如上所示simplexml_load_string的结果里,foo并不是数组,而是一个迭代对象!
可以这样确认:

复制代码 代码如下:


foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);


看来,print_r或者var_dump之类的表象并不完全可信,自己多留心吧。
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template