get the sub index in foreach loop in simpleXMLElement

后端 未结 5 1077
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 13:38


  
    This is title1
    This is desc1         


        
5条回答
  •  心在旅途
    2020-12-19 14:27

    This is because simpleXML structures are not normal arrays but iterators who do not have an array index.

    I don't know of any other solution than keeping track manually using a variable:

     $index = 0;
     $xml = simplexml_load_file('post.xml');
    
     foreach($xml->item as $item )
       {
           ....
           $index++;
        }
    

提交回复
热议问题