php 简单的 html dom 解析器表到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23522784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple html dom parser table to array
提问by loplo
I'm trying to parse the arrivals table from here [1]and put in into an array to be able to format it and put it into a table.
我正在尝试从这里解析到达表[1]并将其放入一个数组中,以便能够对其进行格式化并将其放入一个表中。
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
我在这里和那里做了一些研究,我从其他问题中得到了一些代码,但我无法让数组和表格看起来像我想要的那样。
Anyone can help me out?
任何人都可以帮助我吗?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>
回答by Michelle
Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
也许尝试将每一行放入一个数组中,然后将每个单元格放入另一个数组中。希望这会做你想要的。
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php
library. Get it here!
注意:此解决方案需要simple_html_dom.php
库。到这里来!