Current Location: Home> Function Categories> count

count

Calculate the number of units in an array, or the number of attributes in an object
Name:count
Category:Array
Programming Language:php
One-line Description:Calculates the number of child nodes of the specified node.

Definition and usage

The count() function is used to calculate the number of child nodes of a specified node.

Example

Calculate the number of child nodes of the <car> element:

 <?php
$xml = <<< XML
  <cars>
    <car name="Volvo">
    <child/>
    <child/>
    <child/>
    <child/>
  </car>
  <car name="BMW">
    <child/>
    <child/>
  </car>
</cars>
XML ;

$elem = new SimpleXMLElement ( $xml ) ;
foreach ( $elem as $car )
  {
  printf ( "%s has %d children.<br>" , $car [ 'name' ] , $car -> count ( ) ) ;
  }
?>

Run the instance

grammar

 SimpleXMLElement :: count ( ) ;
Similar Functions
Popular Articles