消除数组范围内的时间戳

时间:2012-12-25 13:00:25

标签: php arrays timestamp

这是我的时间戳数组。我想在30秒内消除值,只有在30秒内没有其他值时才保留值。任何帮助将不胜感激!

Array
(
[99999] => Array
    (
        [0] => 1356399000
        [1] => 1356398971
        [2] => 1356399005
        [3] => 1356413406
    )

[99997] => Array
    (
        [0] => 1356399002
        [1] => 1356399007
        [2] => 1356398871
        [3] => 1356398876
    )

[99996] => Array
    (
        [0] => 1356399003
        [1] => 1356399004
        [2] => 1356399008
    )

[99995] => Array
    (
        [0] => 1356399009
    )

)

我的预期输出:

Array
(
[99999] => Array
    (
        [0] => 1356399000
        [1] => 1356398971
        [2] => 1356413406 
    )

[99997] => Array
    (
        [0] => 1356399002
        [1] => 1356398871
    )

[99996] => Array
    (
        [0] => 1356399003
    )

[99995] => Array
    (
        [0] => 1356399009
    )

)

任何解决方案/建议将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

你的输出是错误的...因为1356398971 + 30 = 1356399001大于1356399000如果我清楚地了解你这应该是什么样的

$data = array(
  99999 => array(
    0 => 1356399000,
    1 => 1356398971,
    2 => 1356399005,
    3 => 1356413406,
  ),
  99997 => array(
    0 => 1356399002,
    1 => 1356399007,
    2 => 1356398871,
    3 => 1356398876,
  ),
  99996 => array(
    0 => 1356399003,
    1 => 1356399004,
    2 => 1356399008,
  ),
  99995 => array(
    0 => 1356399009,
  ),
);

echo "<pre>";



$data = array_map(function ($values) {
    rsort($values);
    $ci = new CachingIterator(new ArrayIterator($values));
    $values = array();
    foreach ( $ci as $ts ) {
        if ($ci->hasNext()) {
            abs($ci->current() - $ci->getInnerIterator()->current()) > 30 and $values[] = $ts;
        } else {
            $values[] = $ts;
        }
    }
    sort($values);
    return $values;
}, $data);

print_r($data);

输出

Array
(
    [99999] => Array
        (
            [0] => 1356398971
            [1] => 1356413406
        )

    [99997] => Array
        (
            [0] => 1356398871
            [1] => 1356399002
        )

    [99996] => Array
        (
            [0] => 1356399003
        )

    [99995] => Array
        (
            [0] => 1356399009
        )

)