PHP count() 函数
PHP 教程
· 2019-01-29 07:13:19
实例
计算 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 教程 文章: https://www.codercto.com/courses/l/5.html