计算 car 节点的子节点个数:
<?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()); } ?>
运行实例 »
定义和用法
count() 函数计算指定节点的子节点个数。
语法
count();