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

У меня есть этот html-код. Я использую Simple 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 House', 'Century City'); $ array [1] = массив ('Dexter Morgan', 'Miami');

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

Related of "как печатать ячейки таблицы с простым html dom"

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

 // 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); 

Он будет работать .. попробуйте это

  include('simple_html_dom.php'); $html = file_get_html('mytable.html'); foreach($html->find('table tr td') as $e){ $arr[] = trim($e->innertext); } print_r($arr); в  include('simple_html_dom.php'); $html = file_get_html('mytable.html'); foreach($html->find('table tr td') as $e){ $arr[] = trim($e->innertext); } print_r($arr); 

Вы можете получить данные из любых атрибутов html-тегов даже …

@lucia nie

Вы должны сделать это так:

 // initialize empty array to store the data array from each row $theData = array(); // loop over rows foreach($html->find('#theTable 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);