PHP SimpleXML count() Function
Description
The count() function counts the children of a specified node.
Syntax
PHP count() Function has the following syntax.
count();
Return
Returns the number of children of an element
Example
Count the children of the book nodes:
<?php// ww w .j a v a2 s. co m
$xml=<<<XML
<books>
<book name="PHP">
<child/>
</book>
<book name="Java">
<child/>
<child/>
</book>
</books>
XML;
$elem=new SimpleXMLElement($xml);
foreach ($elem as $book){
printf("%s has %d children.\n", $book['name'], $book->count());
}
?>
The code above generates the following result.