如果相隔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 )
)
答案 0 :(得分:1)
使用strtotime获取时间戳,然后比较时间戳。
答案 1 :(得分:0)
将日期存储为时间();
...然后
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分钟,避免重复输入......我想你不能想要更多。
答案 2 :(得分: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;
}
结论
这取决于元素中日期的位置....