Monday, 6 March 2017

php - how to print cells of a table with simple html dom



i have this html code. Im using Simple HTML Dom to parse the data into my own php script.

















Name City
Greg House Century City
Dexter Morgan Miami




I need to get the text inside the TDs in an array, example:



$array[0] = array('Greg House','Century City');
$array[1] = array('Dexter Morgan','Miami');



I've tried several ways to get that but i've fail in each and everyone of them. Can someone give me a hand?


Answer



This should do:



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...