Accessing nth element of XML in PHP with SimpleXml

后端 未结 1 1938
感动是毒
感动是毒 2021-01-21 08:07

I have an xml formatted like this:



  
    text
  
  

        
相关标签:
1条回答
  • 2021-01-21 09:09

    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.

    0 讨论(0)
提交回复
热议问题