PHP SimpleXMLElement::key() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::key() returns the name of the child element that the iterator currently points to.

Unlike an array, where the key is a number, the key of a SimpleXMLElement child is its element name. If several children share a name, the same key repeats. In a foreach loop you get the same value from foreach ($xml as $name => $child), which is the preferred way.

Syntax

Call SimpleXMLElement::key() with the values you need. Start with this line.

PHP
echo $xml->key();
CallWhat it does
$xml->key()Returns the element name of the current child, such as item or drink.
foreach ($xml as $name => $child)The simpler way to get the name of every child while looping.
$xml->current()Returns the current child itself, while key() returns its name.

key() returns a name, not a position. The same name can appear many times, so do not use it as a unique array index. Call rewind() first, then check valid() before reading the key.

Library declarations
PHP
SimpleXMLElement::key(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringThe element name of the current child, for example item.
falseReturned when the iterator does not point to a child.

PHP Examples

Example 1 — Print the Name of Each Child

Loop with the iterator methods and print the name and text of each child element.

PHP
<?php
$xml = simplexml_load_string(
    '<menu><item>Tea</item><item>Coffee</item><drink>Juice</drink></menu>'
);

for ($xml->rewind(); $xml->valid(); $xml->next()) {
    echo $xml->key(), ' => ', $xml->current(), PHP_EOL;
}

How It Works

  1. The menu element has three children: two item elements and one drink element.
  2. rewind() starts at the first child, and valid() keeps the loop going while a child exists.
  3. key() returns the element name and current() returns the element, so echo prints both.
  4. The name item appears twice, which shows that keys are names and can repeat.

Example 2 — Collect the Unique Names

Because keys repeat, array_unique() is handy when you want a list of the different element names.

PHP
<?php
$xml = simplexml_load_string(
    '<menu><item>Tea</item><item>Coffee</item><drink>Juice</drink></menu>'
);

$names = [];
for ($xml->rewind(); $xml->valid(); $xml->next()) {
    $names[] = $xml->key();
}

echo 'All: ', implode(', ', $names), PHP_EOL;
echo 'Unique: ', implode(', ', array_unique($names)), PHP_EOL;

How It Works

  1. The loop stores the result of key() for every child in the names array.
  2. The array holds item twice, because two children have that name.
  3. implode() joins the full list into one line, which shows the repeated name.
  4. array_unique() removes the duplicate, so only item and drink remain.

Example 3 — The Simpler Way: foreach with a Key

foreach gives you the same name as key() without any manual iterator calls.

PHP
<?php
$xml = simplexml_load_string(
    '<menu><item>Tea</item><item>Coffee</item><drink>Juice</drink></menu>'
);

foreach ($xml as $name => $child) {
    echo $name, ' => ', $child, PHP_EOL;
}

How It Works

  1. foreach ($xml as $name => $child) goes through every child of menu.
  2. PHP calls rewind(), valid(), and next() for you in the background.
  3. $name holds the same value that key() would return, which is the element name.
  4. The output matches the manual loop, with less code and fewer chances for mistakes.

Frequently Asked Questions

It returns the element name of the child that the iterator currently points to. It is one of the methods that make SimpleXMLElement work as an iterator.
No. The key is the element name, such as item. If several children have the same name, the key repeats.
Write foreach ($xml as $name => $child). The variable $name holds the element name of each child, so you do not need to call key() yourself.
Before rewind(), the iterator does not point to a child. After rewind(), valid() tells you whether a first child exists, and key() then returns its name.
key() returns the name of the current child of the object. getName() returns the name of the element you call it on.

Next: SimpleXMLElement::next()

SimpleXMLElement::next() moves the iterator forward to the next child element.

SimpleXMLElement::next() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful