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

时间:2016-01-07 18:22:16

标签: php xml xpath simplexml

我正在将旧的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

我看起来很高和很低的解决方案,而且我似乎要绕圈子。

非常感谢任何帮助。

干杯

尼科

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:)

<?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";
}