PHP SimpleXMLIterator - key() Method
The PHP SimpleXMLIterator::key() method returns current key. This method gets the XML tag name of the current element.
Syntax
public SimpleXMLIterator::key()
Parameters
No parameter is required.
Return Value
Returns the XML tag name of the element referenced by the current SimpleXMLIterator object or false.
Example: get the current XML tag key
The example below shows the usage of SimpleXMLIterator::key() 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); echo var_dump($xmlIterator->key()); //rewind to the first element $xmlIterator->rewind(); echo var_dump($xmlIterator->key()); //advance to the next element $xmlIterator->next(); echo var_dump($xmlIterator->key()); //advance to the next element $xmlIterator->next(); echo var_dump($xmlIterator->key()); //advance to the next element $xmlIterator->next(); echo var_dump($xmlIterator->key()); ?>
The output of the above code will be:
bool(false) string(2) "To" string(4) "From" string(7) "Subject" string(4) "body"
❮ PHP SimpleXML Reference