このコンテンツを含む単純な html ファイルがあります。
<table border="1">
<tr>
<td>Name</td>
<td>City</td>
</tr>
<tr>
<td>Greg House</td>
<td>Century City</td>
</tr>
<tr>
<td>Dexter Morgan</td>
<td>Miami</td>
</tr>
</table>
私はこのスクリプトを持っています:
include_once('simple_html_dom.php');
//$html = file_get_html('');
$html = file_get_html('http://localhost/path2mylocalfile/test1.html');
// first check if $html->find exists
if (method_exists($html,"find"))
{
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html'))
{
foreach ($html->find('table') as $table)
{
// 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)
{
// initialize array to store the cell data from each col
//$colData = array();
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
if ($rowData) $theData[]=$rowData;
}
//print_r($theData);
}
}
}
//Show array
//foreach ($colData as $colItems)
//{
foreach ($rowData as $rowItems)
{
echo "$rowItems<br /><br />";
}
//print_r($theData);
//}
ループのみを使用する$rowData
と、姓と都市しか表示されないため、ループを追加しようとしました$colData
が、これを使用すると画面に何も表示されません。
行と列をループして、名前と都市の両方を表示するにはどうすればよいですか?