Use a piece of code to reproduce the problem.
At first glance, the results are confusing:
Copy codeThe Code is as follows:
<? Php
$ String = <EOF
<Data>
<Foo> <bar> hello </bar> </foo>
<Foo> <bar> world </bar> </foo>
</Data>
EOF;
$ Data = simplexml_load_string ($ string );
Print_r ($ data );
Print_r ($ data-> foo );
?>
At first glance, the results are confusing:
Copy codeThe Code is as follows:
SimpleXMLElement Object
(
[Foo] => Array
(
[0] => SimpleXMLElement Object
(
[Bar] => hello
)
[1] => SimpleXMLElement Object
(
[Bar] => world
)
)
)
SimpleXMLElement Object
(
[Bar] => hello
)
Obviously, print_r shows that foo is an array with two bar elements, but only one bar element is displayed!
The reason is actually very simple. In the result of simplexml_load_string shown above, foo is not an array but an iteration object!
You can confirm as follows:
Copy codeThe Code is as follows:
Foreach ($ data-> foo as $ v) print_r ($ v );
Foreach ($ data-> children () as $ v) print_r ($ v );
It seems that the representation of print_r or var_dump is not completely credible. Please pay attention to it.