I have an xml formatted like this:
text
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.