gpt4 book ai didi

php - 简单的 html dom 解析器表到数组

转载 作者:太空狗 更新时间:2023-10-29 13:40:15 26 4
gpt4 key购买 nike

我正在尝试从此处 [1] 解析到达表并放入一个数组中,以便能够对其进行格式化并将其放入表格中。

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

谁能帮帮我?

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

最佳答案

也许尝试将每一行放入一个数组,然后将每个单元格放入另一个数组。希望这会如您所愿。

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 库。 Get it here!

关于php - 简单的 html dom 解析器表到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23522784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com