Forum Moderators: coopster
<data type="forecast">
(blah blah blah...)
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p12h-n14-1</layout-key>
<start-valid-time period-name="Thursday">2020-01-23T06:00:00-05:00</start-valid-time>
<start-valid-time period-name="Thursday Night">2020-01-23T18:00:00-05:00</start-valid-time> $arr['2020-01-23T06:00:00-05:00'] = 'Thursday';
$arr['2020-01-23T18:00:00-05:00'] = 'Thursday Night'; // Was expecting an array like:
// $matches = array('Thursday', '2020-01-23T06:00:00-05:00', 'Thursday Night', '2020-01-23T18:00:00-05:00');
preg_match_all('#
<data.type="forecast">
.*?
<layout-key>k-p12h-n14-1</layout-key>
.*?
((<start-valid-time.period-name="([^"]+)">([^"]+)</start-valid-time>)+.*?)
#msix',
$contents,
$matches); $arr = array();
// remove everything except for the <start-valid-time...> elements
$thisContents = preg_replace('#
.*?
<data.type="forecast">
.*?
<layout-key>k-p12h-n14-1</layout-key>
.*?
(
(<start-valid-time.period-name="([^"]+)">([^"]+)</start-valid-time>)+
.*?
)
</time-layout>.*
#msix',
'$1',
$contents);
// remove the opening <start-valid-time...> tag
$thisContents = preg_replace('#\s*<start-valid-time period-name="#i',
'',
$thisContents);
// explode on the ending tag to create
// array('Thursday">2020-01-23T06:00:00-05:00', 'Thursday Night">2020-01-23T18:00:00-05:00')
$matches = explode('</start-valid-time>', $thisContents);
// loop through $matches, explode on ">, then create $arr value
foreach ($matches as $key) {
list($thisKey, $thisVal) = explode('">', $key);
$arr[$thisVal] = $thisKey;
}
print_r($arr);
preg_match_all('#
<data.type="forecast">
.*?
<layout-key>k-p12h-n14-1</layout-key>
.*?
((<start-valid-time.period-name="([^"]+)">([^"]+)</start-valid-time>)+.*?)
#msix',
$contents,
$matches);
array('Thursday', '2020-01-23T06:00:00-05:00', 'Thursday Night', '2020-01-23T18:00:00-05:00')Why isn't this kind of thing done as a two-dimensional array?
array(
array('Thursday','2020-01-23T06:00:00-05:00'),
array('Thursday Night','2020-01-23T18:00:00-05:00')
)and so on. It isn't a list of 28 things, but of 14 sets of two things.