First use a piece of code to reproduce the problem
At first glance, the results are puzzling:
$data = simplexml_load_string($string);
print_r($data);
print_r($data->foo);
?>
At first glance, the results are puzzling:
Obviously print_r shows that 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:
It seems that the appearance of print_r or var_dump is not completely trustworthy, so please pay more attention to it.
Suppose the XML data we obtain is as follows: (can be obtained using curl, fsockopen, etc.)
Get through simplexml_load_string:
[key] => 你好
[pos] => SimpleXMLElement Object
(
)
[acceptation] => Array;Array;Array;
[sent] => Array
(
[0] => SimpleXMLElement Object
(
[orig] => Haven't seen you for a long time. How are you?
[trans] => 多日不见了,你好吗?
)
[1] => SimpleXMLElement Object
(
[orig] => Hello! How are you?
[trans] => 嘿,你好?
)
[2] => SimpleXMLElement Object
(
[orig] => Hello, Brooks!How are you?
[trans] => 喂,布鲁克斯!你好吗?
)
[3] => SimpleXMLElement Object
(
[orig] => Hi, Barbara, how are you?
[trans] => 嘿,芭芭拉,你好吗?
)
[4] => SimpleXMLElement Object
(
[orig] => How are you? -Quite well, thank you.
[trans] => 你好吗?-很好,谢谢你。
)
)
)
我们在PHP语言中可以用以下方法取得我们想要的值: