PHP Function Reference

PHP SimpleXMLIterator - rewind() Method



The PHP SimpleXMLIterator::rewind() method rewinds the SimpleXMLIterator to the first element.

Syntax

public SimpleXMLIterator::rewind()

Parameters

No parameter is required.

Return Value

No value is returned.

Example:

The example below shows the usage of SimpleXMLIterator::rewind() method.

<?php
$xml = <<<XML
<mail> 
  <To>John Smith</To>
  <From>Marry G.</From>
  <Subject>Happy Birthday</Subject>
  <body>Happy birthday. Live your life with smiles.</body>
</mail> 
XML;

$xmlIterator  = new SimpleXMLElement($xml);

//rewind to first element
$xmlIterator->rewind(); 

//return the current element
var_dump($xmlIterator->current());

//advance to the next element
$xmlIterator->next(); 

//return the current element
var_dump($xmlIterator->current());
?>

The output of the above code will be:

object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(10) "John Smith"
}
object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(8) "Marry G."
}

❮ PHP SimpleXML Reference