简单的 html dom 解析器表到数组 [英] Simple html dom parser table to array

查看:33
本文介绍了简单的 html dom 解析器表到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从这里解析到达表 [1] 并放入数组以便能够对其进行格式化并将其放入表格中.

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.

我在这里和那里做了一些研究,我从其他问题中得到了一些代码,但我无法让数组和表格看起来像我想要的那样.

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.

谁能帮帮我?

<?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>";
}
?>

推荐答案

也许可以尝试将每一行放入一个数组中,然后将每个单元格放入另一个数组中.希望这能满足您的需求.

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>';

注意:此解决方案需要 simple_html_dom.php 库.从这里获取!

这篇关于简单的 html dom 解析器表到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆