Search code examples
phpforeachnodesdomxpath

how to put both nodes in same cycle?


This is what I use:

libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTMLFile("https://www.gismeteo.ru/city/weekly/4230/");
$xpath = new DomXPath($dom);
$weekday= $xpath->query('//*[@id="weather-weekly"]//div[@class="weekday"]');
$date= $xpath->query('//*[@id="weather-weekly"]//div[@class="s_date"]');

foreach ($weekday as $node4){
foreach ($date as $node3){
echo $node4->nodeValue,$node3->nodeValue,"<br>";}}

$node4->nodeValue prints out day of the week sun, mon, tue... and $node3->nodeValue prints out day of the month, how do I make it print everything in the same column like this sat 23.07, sun 24.08...? Thanks.


Solution

  • $xpath = new DomXPath($dom);
    $days = $xpath->query('//*[@id="weather-weekly"]//div[@class="wbshort"]');
    foreach ($days as $day) {
       // find weekday and date under day
       $weekday= $xpath->query('.//div[@class="weekday"]', $day);
       $date= $xpath->query('.//div[@class="s_date"]', $day);
       echo $weekday->item(0)->nodeValue . " " . $date->item(0)->nodeValue;
    }