从时间戳排序和分组数组

时间:2013-12-16 14:32:37

标签: php arrays sorting timestamp

我有以下数组:

Array
(
    [0] => Array
        (
            [dest_in_id] => 1
            [dest_user_id] => 37251
            [dest_inv_user_id] => 37247
            [dest_timestamp] => 1387168510
            [dest_destination_id] => 64
        )

    [1] => Array
        (
            [gi_in_id] => 3
            [gi_user_id] => 37251
            [gi_inv_user_id] => 14564
            [gi_timestamp] => 1345220045
            [gi_group_id] => 2
        )

    [2] => Array
        (
            [dest_in_id] => 2
            [dest_user_id] => 37251
            [dest_inv_user_id] => 37257
            [dest_timestamp] => 1387168510
            [dest_destination_id] => 64
        )

    [3] => Array
        (
            [gi_in_id] => 3
            [gi_user_id] => 37251
            [gi_inv_user_id] => 14564
            [gi_timestamp] => 1345220045
            [gi_group_id] => 2
        )

)

需要查看每个数组的时间戳差异,如果< = day(86400),则创建一个数组类型:

Array
(
    [1387168510] => Array
        (
            [0] => Array
                (
                    [dest_in_id] => 1
                    [dest_user_id] => 37251
                    [dest_inv_user_id] => 37247
                    [dest_timestamp] => 1387168510
                    [dest_destination_id] => 64
                )

            [1] => Array
                (
                    [dest_in_id] => 2
                    [dest_user_id] => 37251
                    [dest_inv_user_id] => 37257
                    [dest_timestamp] => 1387168510
                    [dest_destination_id] => 64
                )
        )

    [1345220045] => Array
        (
            [0] => Array
                (
                    [gi_in_id] => 3
                    [gi_user_id] => 37251
                    [gi_inv_user_id] => 14564
                    [gi_timestamp] => 1345220045
                    [gi_group_id] => 2
                )

            [1] => Array
                (
                    [gi_in_id] => 3
                    [gi_user_id] => 37251
                    [gi_inv_user_id] => 14564
                    [gi_timestamp] => 1345220045
                    [gi_group_id] => 2
                )
        )

)

重要提示!键可能不同!

立刻吸引了函数usort。想做这样的事情:

        usort($aInvitesRows, function($a, $b) {
            $akey = array_keys($a);
            $bkey = array_keys($b);

            if (($b[preg_grep("/(.*?)_timestamp/", $bkey)[3]] - $a[preg_grep("/(.*?)_timestamp/", $akey)[3]]) <= 86400) {
                return $b[preg_grep("/(.*?)_timestamp/", $bkey)[3]] =  array($a, $b);
            }
        });

这次冒险的成功并未加冕:( 在这个阶段,使用usort只能按时间戳(*)排序,你做什么 - 不知道帮助的地址!

*

        usort($aInvitesRows, function($a, $b) {
            $akey = array_keys($a);
            $bkey = array_keys($b);

            return strcmp($b[preg_grep("/(.*?)_timestamp/", $bkey)[3]], $a[preg_grep("/(.*?)_timestamp/", $akey)[3]]);
        });

2 个答案:

答案 0 :(得分:0)

function msort($array, $key, $sort_flags = SORT_REGULAR) {
        if (is_array($array) && count($array) > 0) {
            if (!empty($key)) {
                $mapping = array();
                foreach ($array as $k => $v) {
                    $sort_key = '';
                    if (!is_array($key)) {
                        $sort_key = $v[$key];
                    } else {
                        // @TODO This should be fixed, now it will be sorted as string
                        foreach ($key as $key_key) {
                            $sort_key .= $v[$key_key];
                        }
                        $sort_flags = SORT_STRING;
                    }
                    $mapping[$k] = $sort_key;
                }
                asort($mapping, $sort_flags);
                $sorted = array();
                foreach ($mapping as $k => $v) {
                    $sorted[] = $array[$k];
                }
                return $sorted;
            }
        }
        return $array;
    }

答案 1 :(得分:0)

    $arr = $aInvitesRows;
    $result = array();
    $item = array_shift($arr);
    $akeys = array_keys($item);
    $dt = new \DateTime();

    $dt->setTimestamp($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]);
    $dt->setTime(0, 0, 0);
    $result[$dt->getTimestamp()] = array($item);
    foreach ($arr as $item) {
        $akeys = array_keys($item);
        $f = false;
        foreach ($result as $day => $items) {

            $diff = ($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]) - $day;
            if ($diff < 86400 && $diff > 0) {

                $result[$day][] = $item;
                $f = true;
                break 1;
            }
        }
        if (!$f) {

            $dt = new \DateTime();
            $dt->setTimestamp($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]);
            $dt->setTime(0, 0, 0);
            $result[$dt->getTimestamp()] = array($item);
        }
    }