<div class="reviews-summary__stats">
<div class="reviews-summary">
<p class="reviews-title">A</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">B</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">C</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
</div>
我想迭代每个 div.reviews-summary ,但我没有进入下一个 p.reviews-title 和 li.rating__item rating__rated 标记,并将li.rating__item rating__rated
计为单独的p
标记,并显示ul
中存在的 li.rating__item rating__rated 的整数值。并检查是否存在A
,然后显示A的<{1}}。
li.rating__item rating__rated
我希望像这种类型的输出一样显示
的阵
(
[overalldata] =&gt;排列
(
[a] =&gt; 4
[b] =&gt; 5
[c] =&gt; 4
)
)
任何有任何想法的机构请帮忙解决。感谢
答案 0 :(得分:1)
你的HTML(希望)有些错误。您有三组<p class="reviews-title">C</p>
,因此所有项目都是“C”。但是如果用A和B替换它们,可以使用以下代码。
所有这一切都是在li[class=rating__item rating__rated]
值的小写版本的索引下存储<p class="reviews-title">
个项目的数量......
$final_array=array();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
$item = trim($factor->find('p[class=reviews-title]',0)->plaintext);
$factor = count($factor->find('li[class=rating__item rating__rated]'));
if($item == 'A')
{
$final_array['overalldata']['a']= $factor;
}
if($item == 'B')
{
$final_array['overalldata']['b']= $factor;
}
if($item == 'C')
{
$final_array['overalldata']['c']= $factor;
}
}
print_r($final_array);
我已将HTML更新为(所以只需更换A和B)......
<div class="reviews-summary__stats">
<div class="reviews-summary">
<p class="reviews-title">A</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">B</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">C</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
</div>
这输出......
Array
(
[overalldata] => Array
(
[a] => 3
[b] => 4
[c] => 2
)
)