当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SimpleXMLIterator rewind()用法及代码示例


SimpleXMLIterator::rewind()函数是PHP中的内置函数,用于将SimpleXMLIterator倒退到第一个元素。

用法:

void SimpleXMLIterator::rewind( void )

参数:该函数不接受任何参数。


返回值:该函数不返回任何值。

以下示例程序旨在说明PHP中的SimpleXMLIterator::rewind()函数:

示例1:

<?php 
  
// Store the xml element to variable 
$xml = <<<XML 
    <organization> 
        <name>GeeksforGeeks</name> 
        <address>Noida India</address> 
        <contact> 
            <email>abc@geeksforgeeks.org</email> 
            <mobile>+91-987654321</mobile> 
        </contact> 
    </organization> 
XML; 
  
$xmlIt = new SimpleXMLIterator($xml); 
  
// Use rewind() function to rewind 
// to the first element 
$xmlIt->rewind(); 
  
// Use next() functin to move to the 
// next element 
$xmlIt->next(); 
$xmlIt->next(); 
  
// Display result 
var_dump($xmlIt->current()); 
  
?>
输出:
object(SimpleXMLIterator)#2 (2) {
  ["email"]=>
  string(21) "abc@geeksforgeeks.org"
  ["mobile"]=>
  string(13) "+91-987654321"
}

示例2:

<?php 
  
// Store the xml element to variable 
$xml = <<<XML 
    <organization> 
        <name>GeeksforGeeks</name> 
        <address>Noida India</address> 
        <contact> 
            <email>abc@geeksforgeeks.org</email> 
            <mobile>+91-987654321</mobile> 
        </contact> 
    </organization> 
XML; 
  
$xmlIt = new SimpleXMLIterator($xml); 
  
// Loop starts from first element of xml and  
// run upto when elements are not valid 
for( $xmlIt->rewind(); $xmlIt->valid(); $xmlIt->next() ) { 
      
    var_dump($xmlIt->key()); 
} 
  
?>
输出:
string(4) "name"
string(7) "address"
string(7) "contact"

参考: https://www.php.net/manual/en/simplexmliterator.rewind.php



相关用法


注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | SimpleXMLIterator rewind() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。