SimpleXMLIterator::key

(PHP 5, PHP 7, PHP 8)

SimpleXMLIterator::keyReturn current key

说明

public SimpleXMLIterator::key(): mixed

This method gets the XML tag name of the current element.

参数

此函数没有参数。

返回值

Returns the XML tag name of the element referenced by the current SimpleXMLIterator object or false

范例

示例 #1 Get the current XML tag key

<?php
$xmlIterator 
= new SimpleXMLIterator('<books><book>PHP basics</book><book>XML basics</book></books>');

echo 
var_dump($xmlIterator->key());
$xmlIterator->rewind(); // rewind to the first element
echo var_dump($xmlIterator->key());

?>

以上例程会输出:

bool(false)
string(4) "book"

add a note add a note

User Contributed Notes 1 note

up
3
bmafzallmahmud at gmail dot com
6 years ago
<?php
$note
=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;

$xmlIterator = new SimpleXMLIterator($note);
for(
$xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
      echo
"<pre>";
     echo
var_dump($xmlIterator->key());

}
?>
out put :
string(2) "to"
string(4) "from"
string(7) "heading"
string(4) "body"
To Top