PHP 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());
}
?>
$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();
技术细节
返回值: | 返回某个元素的子节点个数。 |
---|---|
PHP 版本: | 5.3+ |
PHP SimpleXML 参考手册