PHP : DOM 解析器迭代 html 代码

PHP : DOM Parser to iterate html code

<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-titleli.rating__item rating__rated 标签,也计算 li.rating__item rating__rated 单独的 p 标签,并显示整数值li.rating__item rating__rated 存在于ul。同时检查A是否存在然后显示A的li.rating__item rating__rated

<?php
    include("simple_html_dom.php");
    $obj = new simple_html_dom();

    foreach ($obj->find('div[class=reviews-summary]') as $factor)
    {
        $item = $factor->find('p[class=reviews-title]')->plaintext;

        if(trim($item) == 'A')
        { 
            $a = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
        }
        if(trim($item) == 'B')
        { 
            $b = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
        }
        if(trim($item) == 'C')
        { 
            $c = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
        }
        $final_array['overalldata'] = array
        (
            'a' => $a,   // no of A have <li class="rating__item rating__rated"></li> 
            'b' => $b,
            'c' => $c,
        );
    }
    print_r($final_array);
    die;
?>

我想要这样的输出, 数组 ( [整体数据] => 数组 ( [a] => 4
[b] => 5
[c] => 4
) )

任何人有任何想法请帮忙整理一下。谢谢

您的HTML(希望)有点错误。您有三组 <p class="reviews-title">C</p>,所以所有项目都是 'C'。但是如果你用A和B替换它们,你可以使用下面的代码。

它所做的只是在 <p class="reviews-title"> 值的小写版本的索引下存储 li[class=rating__item rating__rated] 项的数量...

$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
        )

)