【问题标题】:How I only get the first TDs of each TR, with Simple Html Dom?如何使用 Simple Html Dom 仅获得每个 TR 的第一个 TD?
【发布时间】:2018-09-26 02:14:04
【问题描述】:

我只需要使用 Simple Html Dom 从表中获取所有行的第一列。我该怎么做?

foreach($html->find('table.horarios-filmes-single tr') as $tr) {

    foreach ($tr->find('td', 1) as $element) {
       echo $element->plaintext . '<br>';
    }
}

这会返回错误:

【问题讨论】:

    标签: php foreach html-table find simple-html-dom


    【解决方案1】:

    这应该可以帮助您解决问题,

    $e = []; 
    foreach($html->find('table.horarios-filmes-single tr') as $tr) {
        $e[] = $tr->find('td', 0); 
    }
    

    0 索引将在每个tr 中为您提供td 的第一个实例

    如果仍然遇到任何问题,您应该print_r($html-&gt;find('table.horarios-filmes-single tr')); 并检查您是否收到任何数据。

    【讨论】: