PHP count() 函数
PHP count() 方法计算指定节点的子节点个数
( PHP >= 5.3 )
方法原型
count();
方法返回值
返回某个元素的子节点个数
范例
下面的范例计算 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()); }
运行以上 PHP 范例,输出结果如下
Volvo has 4 children. BMW has 2 children.