PHP / XML - 不同的值和嵌套循环

PHP / XML - distinct values & nested loops

我正在将旧的 asp 电影院网站升级到 php。它需要从 xml 提要中提取其电影时间表数据。

这是schedule.xml:

<showtimes>
  <movie>
    <name>BROOKLYN</name>
    <movieId>BR30015</movieId>
    <showtime>
      <date>01082016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29779</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29780</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29797</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29796</linkref>
    </showtime>
    <showtime>
      <date>01102016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29822</linkref>
    </showtime>
    <showtime>
      <date>01102016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29823</linkref>
    </showtime>
  </movie>
  <movie>
    <name>THE HATEFUL EIGHT</name>
    <movieId>HA18700</movieId>
    <showtime>
      <date>01072016</date>
      <time>1630</time>
      <soldout>0</soldout>
      <linkref>29399</linkref>
    </showtime>
    <showtime>
      <date>01072016</date>
      <time>2000</time>
      <soldout>1</soldout>
      <linkref>29400</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>1800</time>
      <soldout>0</soldout>
      <linkref>29770</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>2130</time>
      <soldout>0</soldout>
      <linkref>29771</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>2330</time>
      <soldout>0</soldout>
      <linkref>29799</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1800</time>
      <soldout>0</soldout>
      <linkref>29808</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>2130</time>
      <soldout>0</soldout>
      <linkref>29807</linkref>
    </showtime>
  </movie>
</showtimes>

想法是在电影详细信息页面中有一个放映时间部分,用户可以在其中单击日期/时间并直接转到该放映的在线预订网站。

电影是通过 movieId 标签从 xml 中提取的。 每部电影每天放映 3-5 次,持续至少一周。

如果用户选择电影《八恶人》,我需要这样显示输出:

01072016 - 1630, 2000, 
01082016 - 1800, 2130, 2330
01092016 - 1800, 2130

四处寻找解决方案,似乎在兜圈子。

如有任何帮助,我们将不胜感激。

干杯

妮可

这应该可以解决问题:)

<?php

$xml = "<showtimes>
  <movie>
    <name>BROOKLYN</name>
    <movieId>BR30015</movieId>
    <showtime>
      <date>01082016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29779</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29780</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29797</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29796</linkref>
    </showtime>
    <showtime>
      <date>01102016</date>
      <time>1700</time>
      <soldout>0</soldout>
      <linkref>29822</linkref>
    </showtime>
    <showtime>
      <date>01102016</date>
      <time>1930</time>
      <soldout>0</soldout>
      <linkref>29823</linkref>
    </showtime>
  </movie>
  <movie>
    <name>THE HATEFUL EIGHT</name>
    <movieId>HA18700</movieId>
    <showtime>
      <date>01072016</date>
      <time>1630</time>
      <soldout>0</soldout>
      <linkref>29399</linkref>
    </showtime>
    <showtime>
      <date>01072016</date>
      <time>2000</time>
      <soldout>1</soldout>
      <linkref>29400</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>1800</time>
      <soldout>0</soldout>
      <linkref>29770</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>2130</time>
      <soldout>0</soldout>
      <linkref>29771</linkref>
    </showtime>
    <showtime>
      <date>01082016</date>
      <time>2330</time>
      <soldout>0</soldout>
      <linkref>29799</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>1800</time>
      <soldout>0</soldout>
      <linkref>29808</linkref>
    </showtime>
    <showtime>
      <date>01092016</date>
      <time>2130</time>
      <soldout>0</soldout>
      <linkref>29807</linkref>
    </showtime>
  </movie>
</showtimes>";

function getMovieNode($name, $xmlString) {
  $movies = simplexml_load_string($xmlString);
  foreach ($movies as $movie) {
    if ($movie->name == $name) return $movie;
  }
  throw new Exception('Movie not found');
}

function convertNodes($nodes) {
  $times = [];
  foreach ($nodes as $time) {
    array_push($times, $time);
  }
  return $times;
}

function getSchedule($name, $xmlString) {
  $movie = getMovieNode($name, $xmlString);
  return array_reduce(convertNodes($movie->showtime), function($acc, $curr) {
    $acc[reset($curr->date)][] = reset($curr->time);
    return $acc;
  }, []);
}

foreach (getSchedule('THE HATEFUL EIGHT', $xml) as $date => $times) {
  print $date;
  print ' - ';
  $last = array_pop($times);
  foreach ($times as $time) {
    print $time;
    print ', ';
  }
  print $last . "\n";
}