如何将两个节点放在同一个循环中?

时间:2016-07-18 12:03:57

标签: php foreach nodes domxpath

这就是我使用的:

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打印出一周中的某一天sun, mon, tue...$node3->nodeValue打印出该月的某一天,如何在同一列中打印所有内容,例如sat 23.07, sun 24.08... ?感谢。

1 个答案:

答案 0 :(得分:0)

$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;
}