我怎么搞砸了计算这个对象的属性?
我正在使用 domdocument() 从网页检索数据,并且我想计算匹配的数量:
$dom = new DOMDocument();
@$dom->loadHTML($output);
$xpath = new DOMXPath($dom);
$brands = $xpath->query('//li[@class="cp_item"]/a/p[1]'); // get the contents of the first paragraph inside the link
我(可能是错误的)理解是 $brands 是一个对象,其中匹配是属性。从 PHP.net 评论中,我得到以下内容作为计算对象中属性数量的方法。
$count_brands = count((array) $brands);
即使我可以看到有很多匹配,这也会产生 0
foreach ($brands as $brand) {
echo(trim($tag->nodeValue))
}
显然,我要么误解了数据的存储方式,要么误用了 count() 方法。我刚刚学习 OO PHP,所以这可能是一些愚蠢的事情。
I'm using domdocument() to retrieve data from a web page, and I want to count the number of matches:
$dom = new DOMDocument();
@$dom->loadHTML($output);
$xpath = new DOMXPath($dom);
$brands = $xpath->query('//li[@class="cp_item"]/a/p[1]'); // get the contents of the first paragraph inside the link
My (likely wrong) understanding is that $brands is an object of which the matches are properties. From PHP.net comments I get the following as a way to count the number of properties in the object.
$count_brands = count((array) $brands);
This yields 0 even though I can then see there are many matches using
foreach ($brands as $brand) {
echo(trim($tag->nodeValue))
}
Obviously I'm either misunderstanding how the data is getting stored or misapplying the count() method. I'm just learning OO PHP so it's probably something stupid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个“对象”是一个 DOMNodelist 并且有一个包含数字的属性length的项目。
This "object" is a DOMNodelist and has a property length which contains the number of items.