我有一个API调用。该调用返回XML,我将XML转换为数组,然后使用json_encode在jsonp调用中发回响应。我正在做的是工作,但API不允许我按状态过滤,按开始和结束时间过滤,并确保房间可用。
几个jsponp调用让它变得疯狂。再一次,我所拥有的是工作,但我需要找到一种方法来再次过滤我的结果。我已经返回了正确的状态和正确的日期,但我仍然需要过滤掉时间。如果时间目前是上午10点29分,我不想显示从上午10点30分开始预订的房间,因为它不会在上午10:30之前预订。所以,如果我现在的时间是上午10:29,我需要过滤我的数组中的事件,并且只显示具有当前时间的开始和结束时间的事件。
从上午10点到11点的预订应该留在我的阵列中但是从下午2-4点开始的预订不应该出现在我的阵列中。正如我所说,数组首先是一个返回的XML文档,然后是一个数组,然后是JSON,所以在技术上我可以使用任何工作来过滤。 PHP以XML格式过滤,PHP过滤数组。甚至Javascript也可以通过我的ajax调用来准备PHP文件。
在我将其转换为JSON并返回之前,这是我的数组。我过滤的时间应该是TimeEventStart和TimeEventEnd。我可以使用当前的Javascript时间或PHP日期函数时间。我可以过滤数组或使用类似XMLPath的东西进行过滤。不确定什么是最有效的,并且最有效。
$axml = Array{
"Bookings":{
"Data":[
{
"BookingDate":[
"2014-05-09T00:00:00"
],
"RoomDescription":[
"Room06"
],
"TimeEventStart":[
"2014-05-09T14:00:00"
],
"TimeEventEnd":[
"2014-05-09T14:30:00"
],
"EventName":[
"Jake Long"
],
"SetupTypeDescription":[
"(none)"
],
"ReservationID":[
"137"
],
"OpenTime":[
"1900-01-01T00:00:00"
],
"CloseTime":[
"1900-01-01T00:00:00"
],
"EventTypeDescription":[
[
]
],
"BookingID":[
"709"
],
"TimeBookingStart":[
"2014-05-09T14:00:00"
],
"TimeBookingEnd":[
"2014-05-09T14:30:00"
],
"GMTStartTime":[
"2014-05-09T18:00:00"
],
"GMTEndTime":[
"2014-05-09T18:30:00"
],
"TimeZone":[
"ET"
],
"RoomCode":[
"R06"
],
"Room":[
"Room 6"
],
"RoomID":[
"34"
],
"StatusID":[
"3"
],
"EventTypeID":[
"0"
],
"DateAdded":[
"2014-05-09T13:47:29.087"
],
"DateChanged":[
"2014-05-09T13:47:29.087"
],
"ChangedBy":[
"Admin"
]
},
{
"BookingDate":[
"2014-05-09T00:00:00"
],
"RoomDescription":[
"Room06"
],
"TimeEventStart":[
"2014-05-09T18:00:00"
],
"TimeEventEnd":[
"2014-05-09T20:00:00"
],
"EventName":[
"Rob Brown"
],
"SetupTypeDescription":[
"(none)"
],
"ReservationID":[
"142"
],
"OpenTime":[
"1900-01-01T00:00:00"
],
"CloseTime":[
"1900-01-01T00:00:00"
],
"EventTypeDescription":[
[
]
],
"BookingID":[
"714"
],
"TimeBookingStart":[
"2014-05-09T18:00:00"
],
"TimeBookingEnd":[
"2014-05-09T20:00:00"
],
"GMTStartTime":[
"2014-05-09T22:00:00"
],
"GMTEndTime":[
"2014-05-10T00:00:00"
],
"TimeZone":[
"ET"
],
"RoomCode":[
"R06"
],
"Room":[
"Room 6"
],
"RoomID":[
"34"
],
"StatusID":[
"3"
],
"EventTypeID":[
"0"
],
"DateAdded":[
"2014-05-09T14:58:55.71"
],
"DateChanged":[
"2014-05-09T14:58:55.71"
],
"ChangedBy":[
"Admin"
]
},
{
"BookingDate":[
"2014-05-09T00:00:00"
],
"StartBookingDate":[
"2014-05-09T00:00:00"
],
"RoomDescription":[
"Room06"
],
"TimeEventStart":[
"2014-05-09T11:00:00"
],
"TimeEventEnd":[
"2014-05-09T12:00:00"
],
"EventName":[
"Jimmy James"
],
"SetupTypeDescription":[
"(none)"
],
"ReservationID":[
"141"
],
"OpenTime":[
"1900-01-01T00:00:00"
],
"CloseTime":[
"1900-01-01T00:00:00"
],
"EventTypeDescription":[
[
]
],
"BookingID":[
"713"
],
"TimeBookingStart":[
"2014-05-09T11:00:00"
],
"TimeBookingEnd":[
"2014-05-09T12:00:00"
],
"GMTStartTime":[
"2014-05-09T15:00:00"
],
"GMTEndTime":[
"2014-05-09T16:00:00"
],
"TimeZone":[
"ET"
],
"RoomCode":[
"R06"
],
"Room":[
"Room 6"
],
"RoomID":[
"34"
],
"StatusID":[
"1"
],
"EventTypeID":[
"0"
],
"DateAdded":[
"2014-05-09T14:58:15.17"
],
"DateChanged":[
"2014-05-09T14:58:15.17"
],
"ChangedBy":[
"Admin"
]
}
]
}
}
;
我的数组是从XML中的SOAP调用返回的。我正在使用
获取XML$sxml = simplexml_load_string($xml)
然后我使用一个名为xmlToArray的函数将它放入发布的数组中。
$axml = xmlToArray($sxml);
在转换为数组然后转换为JSON之前,这是XML。
<?xml version="1.0" encoding="utf-8"?><Bookings>
<Data>
<BookingDate>2014-05-13T00:00:00</BookingDate>
<StartBookingDate>2014-05-13T00:00:00</StartBookingDate>
<RoomDescription>Room 06</RoomDescription>
<TimeEventStart>2014-05-13T10:00:00</TimeEventStart>
<TimeEventEnd>2014-05-13T10:30:00</TimeEventEnd>
<EventName>Jake Long</EventName>
<ReservationID>159</ReservationID>
<ClosedAllDay>false</ClosedAllDay>
<OpenTime>1900-01-01T00:00:00</OpenTime>
<CloseTime>1900-01-01T00:00:00</CloseTime>
<BookingID>731</BookingID>
<TimeBookingStart>2014-05-13T10:00:00</TimeBookingStart>
<TimeBookingEnd>2014-05-13T10:30:00</TimeBookingEnd>
<GMTStartTime>2014-05-13T14:00:00</GMTStartTime>
<GMTEndTime>2014-05-13T14:30:00</GMTEndTime>
<RoomCode>R09</RoomCode>
<Room>Room 9</Room>
<RoomID>37</RoomID>
<StatusID>3</StatusID>
<DateAdded>2014-05-13T08:38:22.36</DateAdded>
<DateChanged>2014-05-13T08:38:22.36</DateChanged>
</Data>
</Bookings>
答案 0 :(得分:6)
您可以使用Xpath过滤XML。 Xpath 1.0无法比较字符串,但您也可以注册php函数。
所以用PHP编写条件:
function isBetween($current, $min, $max) {
$current = strtotime($current.'Z');
$min = strtotime($min.'Z');
$max = strtotime($max.'Z');
return $current >= $min && $current <= $max;
}
创建DOM文档并加载XML。创建Xpath实例注册PHP命名空间和函数。
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPhpFunctions('isBetween');
定义使用该函数过滤数据元素节点的Xpath表达式:
// use gmtime() for current time
$now = strtotime('2014-05-13T14:00:00Z');
$expression = sprintf(
'/Bookings/Data[
php:function("isBetween", "%s", string(GMTStartTime), string(GMTEndTime))
]',
gmdate('Y-m-d\\TH:i:s', $now)
);
使用Xpath表达式获取Data元素节点:
$result = [];
foreach($xpath->evaluate($expression) as $dataNode) {
$record = [];
// convert the matching data into a record array
foreach ($xpath->evaluate('*', $dataNode) as $childNode) {
$record[$childNode->localName] = $childNode->nodeValue;
}
$result[] = $record;
}
var_dump($result);
答案 1 :(得分:4)
好的,所以我编写了一些PHP函数来过滤掉你的需求。
要比较php中的日期,最简单的方法是将它们转换为Unix时间。这是第一个功能的作用。 Unix time是自1970年1月1日以来经过的时间(以秒为单位)。转换它们的优势在于您只需使用&lt;和&gt;经营者要比较两次。
下一个函数(nowBetween),查看当前时间是否介于两次($ start和$ end)之间。
然后我拿了你的JSON字符串(因为我在编写这篇文章时并没有真正看到XML源代码)并将其转换为PHP数组。我们可以确定它是数组,因为json_decode中的第二个参数设置为true。
最后但并非最不重要的是,我遍历了&#34;数据&#34;中的所有预订。在您的JSON内容部分,阅读GMTStart和GMTEnd时间,并使用我们的功能检查当前时间是否介于它们之间。如果不是这样,那么我从$ bookings数组中删除预订。
结果只是保留了正确时间的预订。我的输出现在是空的,但那是因为您的预订日期是2014-05-09 ...当我调整它的工作时间。
我希望这个答案能满足你的需求。
function convertDateTime($datetime) {
//FORMAT IS date("YYYY-MM-DDThh:mm:ss")
$datetime = str_replace("T"," ",$datetime);
return date("U",strtotime($datetime));
}
function nowBetween($start,$end) {
$now = gmdate("U");
$start = convertDateTime($start);
$end = convertDateTime($end);
if ($start <= $now && $end > $now)
{
return true;
}
else
{
return false;
}
}
$bookings = '{"Bookings":{"Data":[{"BookingDate":["2014-05-09T00:00:00"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T14:00:00"],"TimeEventEnd":["2014-05-09T14:30:00"],"EventName":["Jake Long"],"SetupTypeDescription":["(none)"],"ReservationID":["137"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"BookingID":["709"],"TimeBookingStart":["2014-05-09T14:00:00"],"TimeBookingEnd":["2014-05-09T14:30:00"],"GMTStartTime":["2014-05-09T18:00:00"],"GMTEndTime":["2014-05-09T18:30:00"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["3"],"EventTypeID":["0"],"DateAdded":["2014-05-09T13:47:29.087"],"DateChanged":["2014-05-09T13:47:29.087"],"ChangedBy":["Admin"]},{"BookingDate":["2014-05-09T00:00:00"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T18:00:00"],"TimeEventEnd":["2014-05-09T20:00:00"],"EventName":["Rob Brown"],"SetupTypeDescription":["(none)"],"ReservationID":["142"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"BookingID":["714"],"TimeBookingStart":["2014-05-09T18:00:00"],"TimeBookingEnd":["2014-05-09T20:00:00"],"GMTStartTime":["2014-05-09T22:00:00"],"GMTEndTime":["2014-05-10T00:00:00"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["3"],"EventTypeID":["0"],"DateAdded":["2014-05-09T14:58:55.71"],"DateChanged":["2014-05-09T14:58:55.71"],"ChangedBy":["Admin"]},{"BookingDate":["2014-05-9T00:00:00"],"StartBookingDate":["2014-05-09T00:00:00"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T11:00:00"],"TimeEventEnd":["2014-05-09T12:00:00"],"EventName":["Jimmy James"],"SetupTypeDescription":["(none)"],"ReservationID":["141"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"BookingID":["713"],"TimeBookingStart":["2014-05-09T11:00:00"],"TimeBookingEnd":["2014-05-09T12:00:00"],"GMTStartTime":["2014-05-09T15:00:00"],"GMTEndTime":["2014-05-09T16:00:00"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["1"],"EventTypeID":["0"],"DateAdded":["2014-05-09T14:58:15.17"],"DateChanged":["2014-05-09T14:58:15.17"],"ChangedBy":["Admin"]}]}}';
$bookings = json_decode($bookings,true);
foreach ($bookings["Bookings"]["Data"] as $index=>$arrContent)
{
if (!nowBetween($arrContent["GMTStartTime"][0],$arrContent["GMTEndTime"][0]))
{
unset($bookings["Bookings"]["Data"][$index]);
}
}
答案 2 :(得分:2)
根据我的理解,您将收到&lt;预订&gt; ?然后你有两个选择:如果记录数总是很大 - 超过几千,那么你应该使用XPath过滤数据之前将数据转换为数组 - 将节点从XPath->查询结果复制到新文档并转换文档到数组。如果记录数量很小,那么之后更容易转换它们 - 只需使用带有类似于此的回调的array_filter:
$filteredResult = array_filter($data, function($row) {
// parse dates and compare them.
});
你将获得过滤后的阵列。