php8.5
Home/ Manual/ simplexml / simplexmlelement/ SimpleXMLElement::getChildren

SimpleXMLElement::getChildren

PHP function Edit on GitHub ✎

(PHP 8)

Returns the sub-elements of the current element

Description

SimpleXMLElement::getChildren(): SimpleXMLElement|null
Warning

Prior to PHP 8.0, SimpleXMLElement::getChildren was only declared on the subclass SimpleXMLIterator.

This method returns a SimpleXMLElement object containing sub-elements of the current SimpleXMLElement element.

Parameters Parameters

Return Values

Returns a SimpleXMLElement object containing the sub-elements of the current element.

Examples

Return the sub-elements of the current element

php
<?php
$xml = <<<XML
<books>
    <book>
        <title>PHP Basics</title>
        <author>Jim Smith</author>
    </book>
    <book>XML basics</book>
</books>
XML;

$xmlElement = new SimpleXMLElement($xml);
for ($xmlElement->rewind(); $xmlElement->valid(); $xmlElement->next()) {
    foreach($xmlElement->getChildren() as $name => $data) {
    echo "The $name is '$data' from the class " . get_class($data) . "\n";
    }
}
?>

The above example will output:

output
The title is 'PHP Basics' from the class SimpleXMLElement
The author is 'Jim Smith' from the class SimpleXMLElement

Source: reference/simplexml/simplexmlelement/getchildren.xml · from the official PHP manual (php/doc-en)