0

如果相隔 3 分钟之内,如何从数组中获取值?这是我的数组,$array1

 Array ( 
[0] => Array ( [id] => 47523 [date] => 12-02-13 14:36:32 ) 
[1] => Array ( [id] => 65454 [date] => 12-02-13 14:36:32 ) 
[2] => Array ( [id] => 47523 [date] => 12-02-13 13:56:48 ) 
[3] => Array ( [id] => 38639 [date] => 12-02-13 13:56:48 ) 
[4] => Array ( [id] => 38639 [date] => 12-02-13 13:08:51 ) 
[5] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 ) 
[6] => Array ( [id] => 38639 [date] => 12-02-13 13:09:43 ) 
)

我怎样才能得到这个输出

Array ( 
[0] => Array ( [id] => 38639 [date] => 12-02-13 13:08:51 ) 
[1] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 ) 
[2] => Array ( [id] => 38639 [date] => 12-02-13 13:09:43 ) 
)
4

3 回答 3

1

使用strtotime获取时间戳,然后比较时间戳。

于 2013-02-13T13:11:48.827 回答
0

不知道你是如何得到你的结果的,但它看起来不对......因为它们可能是 4 个不同的结果。

想象

$data = array(
  0 => Array ( 'id' => 47523, 'date' => "12-02-13 14:36:32" ),
  1 => Array ( 'id' => 47523, 'date' => "12-02-13 13:56:48" ),
  2 => Array ( 'id' => 65454, 'date' => "12-02-13 14:36:32" ),
  3 => Array ( 'id' => 38639, 'date' => "12-02-13 13:56:48" ),
  4 => Array ( 'id' => 38639, 'date' => "12-02-13 13:08:51" ),
  5 => Array ( 'id' => 38639, 'date' => "12-02-13 13:07:43" ),
  6 => Array ( 'id' => 38639, 'date' => "12-02-13 13:09:43" ),
);

例子

var_dump(getDiff($data));
var_dump(getDiff($data,SORT_ASC));
var_dump(getDiff($data,SORT_DESC));

SORT_NONE

array (size=3)
  0 => 
    array (size=2)
      'id' => int 47523
      'date' => string '12-02-13 13:56:48' (length=17)
  1 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:08:51' (length=17)
  2 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:07:43' (length=17)

输出 XSORT_ASC

array (size=4)
  0 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:08:51' (length=17)
  1 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:09:43' (length=17)
  2 => 
    array (size=2)
      'id' => int 47523
      'date' => string '12-02-13 13:56:48' (length=17)
  3 => 
    array (size=2)
      'id' => int 47523
      'date' => string '12-02-13 14:36:32' (length=17)

输出 XSORT_DESC

array (size=4)
  0 => 
    array (size=2)
      'id' => int 65454
      'date' => string '12-02-13 14:36:32' (length=17)
  1 => 
    array (size=2)
      'id' => int 47523
      'date' => string '12-02-13 13:56:48' (length=17)
  2 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:08:51' (length=17)
  3 => 
    array (size=2)
      'id' => int 38639
      'date' => string '12-02-13 13:07:43' (length=17)

使用的功能

function getDiff(array $data, $sort = 0) {
    switch ($sort) {
        case SORT_ASC :
            usort($data, function ($a, $b) {
                $a = strtotime($a['date']);
                $b = strtotime($b['date']);
                return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
            });
            break;
        case SORT_DESC :
            usort($data, function ($a, $b) {
                $a = strtotime($a['date']);
                $b = strtotime($b['date']);
                return ($a == $b) ? 0 : (($a < $b) ? 1 :  -1);
            });

            break;
    }


    $it = new CachingIterator(new ArrayIterator($data), CachingIterator::FULL_CACHE);
    $list = array();
    foreach ( $it as $var ) {
        if ($it->hasNext()) {
            $date = strtotime($var['date']);
            $date2 = $it->getInnerIterator()->current();
            $date2 = strtotime($date2['date']);
            $diff = ($sort === SORT_DESC || $sort === 0) ? $date - $date2  :  $date2 - $date  ;
            if ($diff <= (60 * 3)) {
                $list[] = $it->getInnerIterator()->current();
            }
        }
    }
    return $list;
}

结论

这取决于元素中日期的位置....

于 2013-02-13T13:36:40.993 回答
0

将日期存储为 time();

然后...

function filterArray3minutes($array) {
  foreach($array as $item) {
   foreach($array as $item2) {
     if($item[date] > $item2[date]-3*60 && $item[date] < $item2[date]+3*60 && !isset($array2[$item[id]]) $array2[$item[id]] = $item;
     }
  }
  foreach($array2 as $item) {
     $array3[] = $item;
  }
   return $array3;
}

我认为保持时间是一个很好的做法,因为只有整数例外是输入和输出,它允许更快地使用它。

编辑:现在此功能使项目彼此相隔 3 分钟,并避免重复条目......我认为你不能想要更多。

于 2013-02-13T13:15:27.777 回答