9

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.

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>";
}
?>
4
  • 2
    So these are almost two separate questions. The first, is to parse the data into an array format that you can work with and the second to output the array into a desirable format. It might help to solve the first problem first. In order to do that, it would be useful to see a var_dump($rowData) output along with what data structure you are trying to achieve. Commented May 7, 2014 at 16:07
  • Hey Mike,the dump looks like this (right now): pastebin.com/KD6zP4Ui Commented May 7, 2014 at 21:11
  • that looks like two var dumps next to each other. Which is from $rowData? If it is the second array, then your display code is not working because you have a multi-dimensional array. and echoing an array is just going to give you emptiness. Commented May 7, 2014 at 23:26
  • The actual code looks like this: pastebin.com/KyvcnzZj . And the dump looks like this: pastebin.com/86BtwGc0 . The source of the table is this: flightplan.romatsa.ro/init/fpl/flightslr/LRCL , I'm trying to get the arrivals from there. I'm really appreciating your help. Commented May 8, 2014 at 5:21

1 Answer 1

21

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.