我正在寻找一种方法将多个电影放映时间组合成一个字符串。我们从我们的来源获得的数据有一些特定电影在特定日播放的特定电影的实例,但在XML中有两个列表,因为剧院提供酒精并且正在普通剧院播放电影而且很吵影院。
以下是我们从数据源获得的电影showtime字符串的两个示例:
使用这些showtime字符串,我需要它看起来像这样:
我从字符串中删除重复项是没有问题的(例如8:00),但我正在努力解决如何将字符串最佳地组合成一个单一的,有凝聚力的单元。
有没有人对如何在PHP中执行此操作有任何建议?需要运行的服务器有5.3.something
编辑 - 代码 我不认为这会产生一大堆意义,因为它是我编辑的更大脚本的一部分
//Make a unique key so we can check if a movie listing already exists for this combo
$showtimesKey = implode('_', array(
(string)$currentShowTime->theater_id,
(string)$currentShowTime->movie_id,
(string)$currentListing['date']
));
if (!isset($justShowtimes[$showtimesKey])) {
foreach (explode(", ", trim($currentListing->showtimes)) as $time) {
$justShowtimes[$showtimesKey][$time] = $time;
}
} else {
$previousTimes = $justShowtimes[$showtimesKey];
foreach (explode(", ", trim($currentListing->showtimes)) as $time) {
$justShowtimes[$showtimesKey][$time] = $time;
}
natsort($justShowtimes[$showtimesKey]);
//sort($justShowtimes[$showtimesKey], SORT_STRING);
$currentListing->showtimes = $justShowtimes[$showtimesKey];
}
答案 0 :(得分:1)
也许你可以尝试这样的事情:
$string1 = "8:00, 10:30, 11:40 PM";
$string2 = "(11:20 AM), (4:40), 8:00, 11:40 PM ";
$fullString = $string1.",".$string2;
$splittedString = split(",",$fullString);
$times = array();
foreach($splittedString as $id=>$timeStr) {
$timeStr = (strpos($timeStr,'('))?str_replace(array("(",")"),"",$timeStr)." -1 DAY":$timeStr;
$time = strtotime($timeStr);
$times[$time] = $splittedString[$id];
}
ksort($times);
$orderedString = implode(",",$times);
echo $orderedString ;
答案 1 :(得分:0)
我最终以这种方式解决问题,虽然看起来有点麻烦和过度设计。但我无法想出一个很好的方法来保持AM,Matinee和常规的PM放映时间顺序正确。
$showtimesKey = implode('_', array(
(string)$currentShowTime->theater_id,
(string)$currentShowTime->movie_id,
(string)$currentListing['date']
));
if (!isset($justShowtimes[$showtimesKey])) {
$justShowtimes[$showtimesKey] = (string) $currentListing->showtimes;
} else {
$previousTimes = $currentTimes = array();
foreach (explode(", ", trim($justShowtimes[$showtimesKey])) as $time) {
$previousTimes[$time] = $time;
}
foreach (explode(", ", trim($currentListing->showtimes)) as $time) {
$currentTimes[$time] = $time;
}
//Just in case we need to get at unedited time arrays
$original_times = array(
'previous' => $previousTimes,
'current' => $currentTimes
);
$am = array();
$pm = array('matinee' => array(), 'full_price' => array());
//Previous Times
if ($position = strpos(implode(",", $previousTimes), "AM")) {
$joined_times = implode(", ", $previousTimes);
list($tmp_am, $tmp_pm) = explode(" AM)", $joined_times);
$tmp_am = str_replace(array("(", ")"), '', $tmp_am);
foreach (explode(", ", $tmp_am) as $time) {
$am[$time] = $time;
}
//Set $previousTimes to only the PM times
$previousTimes = array();
foreach (explode(", ", $tmp_pm) as $time) {
if (empty($time)) continue;
$previousTimes[$time] = $time;
}
}
foreach ($previousTimes as $time) {
$time = str_replace(" PM", '', $time);
if ((strpos($time, "(") !== false) || (strpos($time, ")") !== false)) {
//We are removing the parentesis from the time so a) we can make sure we aren't duplicating any and b) so the array key is just the time
$only_time = str_replace(array("(", ")"), '', $time);
$pm['matinee'][$only_time] = $only_time;
} else {
$pm['full_price'][$time] = $time;
}
}
//Current Times
if ($position = strpos(implode(",", $currentTimes), "AM")) {
$joined_times = implode(", ", $currentTimes);
list($tmp_am, $tmp_pm) = explode(" AM)", $joined_times);
$tmp_am = str_replace(array("(", ")"), '', $tmp_am);
foreach (explode(", ", $tmp_am) as $time) {
$am[$time] = $time;
}
//Set $currebtTimes to only the PM times
$currentTimes = array();
foreach (explode(", ", $tmp_pm) as $time) {
if (empty($time)) continue;
$currentTimes[$time] = $time;
}
}
foreach ($currentTimes as $time) {
$time = str_replace(" PM", '', $time);
if ((strpos($time, "(") !== false) || (strpos($time, ")") !== false)) {
//We are removing the parentesis from the time so a) we can make sure we aren't duplicating any and b) so the array key is just the time
$only_time = str_replace(array("(", ")"), '', $time);
$pm['matinee'][$only_time] = $only_time;
} else {
$pm['full_price'][$time] = $time;
}
}
natsort($am); natsort($pm['matinee']); natsort($pm['full_price']);
if (!empty($am)) {
$last = end($am);
$am[$last] .= " AM";
}
if (!empty($pm['full_price'])) {
$last = end($pm['full_price']);
$pm['full_price'][$last] .= " PM";
} else {
//Only try and tack PM onto the last matinee showing if there actually are matinee showings
if (!empty($pm['matinee'])) {
$last = end($pm['matinee']);
$pm['matinee'][$last] .= " PM";
}
}
foreach ($am as $key => $value) {
$am[$key] = "(" . $value . ")";
}
foreach ($pm['matinee'] as $key => $value) {
$pm['matinee'][$key] = "(" . $value . ")";
}
$justShowtimes[$showtimesKey] = '';
if (!empty($am)) {
$justShowtimes[$showtimesKey] = implode(", ", $am);
}
if (!empty($pm['matinee'])) {
if (!empty($justShowtimes[$showtimesKey])) {
$justShowtimes[$showtimesKey] .= ", ";
}
$justShowtimes[$showtimesKey] .= implode(", ", $pm['matinee']);
}
if (!empty($pm['full_price'])) {
if (!empty($justShowtimes[$showtimesKey])) {
$justShowtimes[$showtimesKey] .= ", ";
}
$justShowtimes[$showtimesKey] .= implode(", ", $pm['full_price']);
}
$currentListing->showtimes = $justShowtimes[$showtimesKey];
}