0

这是我的时间戳数组。我想消除彼此 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
    )

)

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

4

1 回答 1

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
        )

)
于 2012-12-25T13:43:49.057 回答