SimpleXMLIterator::hasChildren

(PHP 5, PHP 7, PHP 8)

SimpleXMLIterator::hasChildrenChecks whether the current element has sub elements

Açıklama

public SimpleXMLIterator::hasChildren(): bool

This method checks whether the current SimpleXMLIterator element has sub-elements.

Değiştirgeler

Bu işlevin değiştirgesi yoktur.

Dönen Değerler

true if the current element has sub-elements, otherwise false

Örnekler

Örnek 1 Check whether the current element has sub-elements

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

$xmlIterator = new SimpleXMLIterator$xml );
for( 
$xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
    if(
$xmlIterator->hasChildren()) {
        
var_dump($xmlIterator->current());
    }
}
?>

Yukarıdaki örneğin çıktısı:

object(SimpleXMLIterator)#2 (2) {
  ["title"]=>
  string(10) "PHP Basics"
  ["author"]=>
  string(9) "Jim Smith"
}

add a note add a note

User Contributed Notes 1 note

up
-4
grummfy at gmail dot com
14 years ago
Hello,

just a note for people like me who encounter the problems :

<?php
foreach($xmlIterator as $x)
{
    if(
$x->hasChildren())
    {
       
//never reach
       
echo 'x : ';
       
var_dump($x->current());
    }
    if(
$xmlIterator->hasChildren())
    {
        echo
'iterator : ';
       
var_dump($x->current()); //all time null
       
var_dump($xmlIterator->current());
    }
}
?>
To Top