как распечатать ячейки таблицы с простым HTML dom

у меня есть этот HTML-код. Я использую Простой HTML Dom для парсинга данных в мой собственный сценарий PHP.

<table>
    <tr>
        <td class="header">Name</td>
        <td class="header">City</td>
    </tr>
    <tr>
        <td class="text">Greg House</td>
        <td class="text">Century City</td>
    </tr>
    <tr>
        <td class="text">Dexter Morgan</td>
        <td class="text">Miami</td>
    </tr>
</table>

Я должен получить текст в TDS в массиве, примере:

$array [0] = массив ('Дом Greg', 'Сенчури-Сити'); $array [1] = массив ('Dexter Morgan', 'Майами');

Я попробовал несколько способов получить это, но у меня есть сбой в каждом и каждом из них. Кто-то может дать мне руку?

8
задан Somnath Muluk 11 April 2012 в 11:43
поделиться

1 ответ

Это должно сделать:

// get the table. Maybe there's just one, in which case just 'table' will do
$table = $html->find('#theTable');

// initialize empty array to store the data array from each row
$theData = array();

// loop over rows
foreach($table->find('tr') as $row) {

    // initialize array to store the cell data from each row
    $rowData = array();
    foreach($row->find('td.text') as $cell) {

        // push the cell's text to the array
        $rowData[] = $cell->innertext;
    }

    // push the row's data array to the 'big' array
    $theData[] = $rowData;
}
print_r($theData);
14
ответ дан 5 December 2019 в 08:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: