PHP : 简单 DOM 解析器如何迭代 html 代码

PHP : Simple DOM Parser how to iterate html code

<div class="reviews-summary__stats">
    <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 "></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 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>

我想遍历每个 div.reviews-summary 但我没有进入下一个 p.reviews-titleli.rating__itemrating__rated标签,也算li.rating__itemrating__rated用于显示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] => 3
[b] => 4
[c] => 2
) )
并计算 li.rating__item rating__rated 它,并显示 li.rating__item rating__rated 的整数值存在于列表

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

我做了一些更改,但包含了几个版本,因为它们的数据格式不同。我认为主要问题是,当您使用 find() 时,这可能 return 找到的项目列表,因此在设置 $a 等时,您使用了第二个参数来选择 plaintext 的第一项(使用 ,0),您在查找 $item 值时没有这样做。所以我在这个调用中添加了相同的内容。

$final_array=array();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
    $item = $factor->find('p[class=reviews-title]',0)->plaintext;
    if(trim($item) == 'A')
    {
        $final_array['overalldata']['a'] = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
    }
    if(trim($item) == 'B')
    {
        $final_array['overalldata']['b'] = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
    }
    if(trim($item) == 'C')
    {
        $final_array['overalldata']['c'] = $factor->find('li[class="rating__item rating__rated"]',0)->plaintext;
    }
}
print_r($final_array);

这给出了(用你的样本数据)...

Array
(
    [overalldata] => Array
        (
            [c] => 
        )

)

或者...

$final_array=array();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
    $a = null;
    $b = null;
    $c = null;
    $item = trim($factor->find('p[class=reviews-title]',0)->plaintext);
    $factor = $factor->find('li[class=rating__item rating__rated]');
    $count = count($factor);
    if($item == 'A')
    {
        $a = $factor[0]->plaintext;
    }
    if($item == 'B')
    {
        $b = $factor[0]->plaintext;
    }
    if($item == 'C')
    {
        $c = $factor[0]->plaintext;
    }
    $final_array['overalldata'] = array
    (
        'a' => $a,
        'b' => $b,
        'c' => $c,
        'count' =>$count
    );
}
print_r($final_array);

稍作改动的一组测试数据给出...

Array
(
    [overalldata] => Array
        (
            [a] => 
            [b] => 
            [c] => Some content
        )

)

更新:

我已经更新了第二个示例,我将一些通用代码移到了主体部分。 $count 只是 <li class="rating__item rating__rated"> 项的数量(我认为这就是您的要求)。