Accessing nth element of XML in PHP with SimpleXml

最后都变了- 提交于 2019-12-01 22:18:08

It seems SimpleXML distinguishes between numeric and non-numeric array offsets in a slightly different way to a normal PHP array, so you need to cast your variable to an integer first. (All input from the query string is a string until you tell PHP otherwise.)

$var = intval($_GET['var']);
echo $xml->foo[$var]->bar;

This will turn the string '1' into the integer 1, and should give the result you require.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!