First use a piece of code to reproduce the problem
At first glance, the result is very confusing:
Copy the code The code is as follows:
$string = <<
hello
world
EOF;
$data = simplexml_load_string($string);
print_r($data);
print_r($data->foo);
?>
At first glance, the result is puzzling:
Copy code The 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 displays foo is an array with two bar elements, but in the end 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 iterable object!
You can confirm like this:
Copy code The code is as follows:
foreach ($data->foo as $v ) print_r($v);
foreach ($data->children() as $v) print_r($v);
It seems that representations such as print_r or var_dump are not It’s not entirely trustworthy, so be more careful.
http://www.bkjia.com/PHPjc/322732.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322732.htmlTechArticleFirst use a piece of code to reproduce the problem. At first glance, the result is very confusing: Copy the code and the code is as follows: ? php $string = EOF data foobarhello/bar/foo foobarworld/bar/foo /data EOF; $data...