0

SimpleHTMLDOM を使用していますが、同じ列を持つ 2 つのテーブルがあり、列のタイトルを抽出する必要がありますか?

サンプル画像

以下は、すべてのデータを取得するために使用しているものですが、月と対応する列のタイトル (1 月、2 月など) のみを選択する必要があります。

 $r_tables = $dom->find('table');

foreach($r_tables as $table) {
    $r_cells = $table->find('td');

    foreach($r_cells as $cell) {
        echo $cell->plaintext.'<br />';
    }
}
4

4 に答える 4

1

私はあなたが探しているものだと思います...

$tables = $dom->find('table');

foreach($tables as $table) {
    $r_cells = $table->find('tr');
    $i = 0;
    foreach($r_cells as $row) {
        $cell = $row->find('td');
        if ($i == 0) {
            foreach($cell as $td) { 
                echo $td.'<br />';
            }
        }  
    $i++;  
    }
}
于 2013-02-11T23:39:51.717 に答える
0

最初のテーブルの最初の tr から td を見つけます。

$tds = $dom->find('table', 0)->find('tr', 0)->find('td');

各 td のテキストを配列にマップします。

$headers = array_map(function($td){return $td->text();}, $tds);
于 2013-02-12T00:33:35.967 に答える
0

これはあなたが望むことをするはずです(私が正しく理解していれば):

foreach($r_tables as $table) {
    $r_cells = $table->find('td');

    echo $r_cells[0]->plaintext . '<br />';;
}
于 2013-02-12T00:39:11.787 に答える
0

大雑把な方法は

$i = 0;
foreach($r_tables as $table) {
$r_cells = $table->find('td');

foreach($r_cells as $cell) {
  if($i == 0) {    
    echo $cell->plaintext.'<br />';
  }    
}
$i++;

}

于 2013-02-11T23:02:49.893 に答える