I am currently working on a small project that would convert a work rota from a simple HTML table to a YAML output. The table I am scraping doesn't repeat the date should additional staff members be working the same one, preferring to just display it once. This means that as my PHP script works through the table, for additional staff members on a certain date then no date is set, resulting in an empty value. So far, I have the following:
<?php
include('simple_html_dom.php');
$html = str_get_html('<table>
<tbody>
<tr>
<td>Day</td>
<td>Jack</td>
</tr>
<tr>
<td></td>
<td>Jill</td>
</tr>
<tr>
<td>Night</td>
<td>John</td>
</tr>
</tbody>
</table>');
foreach($html->find('table') as $element) {
$td = array();
foreach( $element->find('tr') as $row) {
$shift = $row->children(0)->plaintext;
$staff = $row->children(1)->plaintext;
echo $shift;
echo "<br />";
echo "Staff: " . $staff;
echo "<br />";
echo "<br />";
}
}
exit;
?>
This outputs as follows:
Day
Staff: Jack
Staff: Jill
Night
Staff: John
What I'm not sure how to do is have PHP use the same variable set from the previous foreach loop if none exists. That way, I could be able to output as follows:
Day
Staff: Jack
Day
Staff: Jill
Night
Staff: John
Would anyone be able to assist? Thanks!