SimpleXMLElement::key

(No version information available, might only be in Git)

SimpleXMLElement::keyReturn current key

说明

public SimpleXMLElement::key(): string
警告

Prior to PHP 8.0, SimpleXMLElement::key() was only declared on the subclass SimpleXMLIterator.

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

参数

此函数没有参数。

返回值

Returns the XML tag name of the element referenced by the current SimpleXMLElement object.

错误/异常

Throws an Error on failure.

更新日志

版本 说明
8.1.0 An Error is now thrown if SimpleXMLElement::key() is called on an invalid iterator. Previously, false was returned.

示例

示例 #1 Get the current XML tag key

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

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

?>

以上示例会输出:

bool(false)
string(4) "book"