php数组过滤掉旧日期

时间:2015-08-05 06:36:47

标签: php arrays date

我有以下数组:

$avail = array($this->item->extraFields->RoomONEDateAvailable->value, $this->item->extraFields->RoomTWODateAvailable->value, $this->item->extraFields->RoomTHREEDateAvailable->value, $this->item->extraFields->RoomFOURDateAvailable->value, $this->item->extraFields->RoomFIVEDateAvailable->value, $this->item->extraFields->RoomSIXDateAvailable->value, $this->item->extraFields->RoomSEVENDateAvailable->value, $this->item->extraFields->RoomEIGHTDateAvailable->value, $this->item->extraFields->RoomNINEDateAvailable->value, $this->item->extraFields->RoomTENDateAvailable->value);

这些是实际值:

Array ( [0] => Thursday, 01 January 1970 [1] => Thursday, 03 September 2015 [2] => Thursday, 01 January 1970 [3] => Thursday, 01 January 1970 [4] => Thursday, 01 January 1970 [5] => Thursday, 01 January 1970 [6] => Thursday, 01 January 1970 [7] => Thursday, 01 January 1970 [8] => Thursday, 01 January 1970 [9] => Thursday, 01 January 1970 ) 

我需要删除所有'星期四,1970年1月1日'数组中的日期,因此它只包含有效日期,我该怎么办呢?

3 个答案:

答案 0 :(得分:3)

尝试:

$avail = array_diff($avail, array('Thursday, 01 January 1970'));

答案 1 :(得分:1)

您无需重建阵列,可以使用unset() [reference]删除阵列条目。
更新:此处不需要引用,您可以直接使用相应的数组:

foreach($avail as $key => $val)
{
    if($val == "Thursday, 01, January 1970")
        unset($avail[$key]);
}

var_dump($avail);

这是经过测试的,应该适合你。

答案 2 :(得分:0)

您可以使用array_keys()函数,添加搜索特定值的选项,如下所示:

array_keys($array, "Thursday, 01 January 1970")

它将返回一个匹配索引数组,您应该从原始数组中取消设置。