1

我有一组包含买卖交易的数据。我正在尝试根据日期对交易进行分组,然后将买卖合并到他们自己的组中。我的输入如下:

   Array
(
    [0] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-02-14
            [instrument] => USD/CAD
            [units] => 2000.0
            [gbp_amount] => 12.4869
            [costs] => 0
            [duty] => 0
        )

    [1] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-02-14
            [instrument] => USD/CAD
            [units] => 2000.0
            [gbp_amount] => 14.62840335
            [costs] => 0
            [duty] => 0
        )

    [2] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-02-15
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 24.80064
            [costs] => 0
            [duty] => 0
        )

    [3] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-02-15
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 30.32498256
            [costs] => 0
            [duty] => 0
        )

    [4] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-02-21
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 24.63168
            [costs] => 0
            [duty] => 0
        )

    [5] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-02-21
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 26.633004
            [costs] => 0
            [duty] => 0
        )

    [6] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-02-21
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 24.63168
            [costs] => 0
            [duty] => 0
        )

    [7] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-02-22
            [instrument] => USD/CAD
            [units] => 4000.0
            [gbp_amount] => 27.0270324
            [costs] => 0
            [duty] => 0
        )

    [8] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-02-21
            [instrument] => USD/CAD
            [units] => 2000.0
            [gbp_amount] => 12.31584
            [costs] => 0
            [duty] => 0
        )

    [9] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-02-22
            [instrument] => USD/CAD
            [units] => 2000.0
            [gbp_amount] => 13.88811768
            [costs] => 0
            [duty] => 0
        )

    [10] => stdClass Object
        (
            [action] => Buy
            [date] => 2011-04-21
            [instrument] => USD/CAD
            [units] => 1000.0
            [gbp_amount] => 12.09882
            [costs] => 0
            [duty] => 0
        )

    [11] => stdClass Object
        (
            [action] => Sell
            [date] => 2011-04-22
            [instrument] => USD/CAD
            [units] => 1000.0
            [gbp_amount] => 13.87129713
            [costs] => 0
            [duty] => 0
        )

    [12] => stdClass Object
        (
            [action] => Buy
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.8517069
            [costs] => 0
            [duty] => 0
        )

    [13] => stdClass Object
        (
            [action] => Sell
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.8517069
            [costs] => 0
            [duty] => 0
        )

    [14] => stdClass Object
        (
            [action] => Buy
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 27.0
            [gbp_amount] => 0.68136552
            [costs] => 0
            [duty] => 0
        )

    [15] => stdClass Object
        (
            [action] => Sell
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 27.0
            [gbp_amount] => 0.68136552
            [costs] => 0
            [duty] => 0
        )

    [16] => stdClass Object
        (
            [action] => Buy
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.34068276
            [costs] => 0
            [duty] => 0
        )

    [17] => stdClass Object
        (
            [action] => Sell
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.34068276
            [costs] => 0
            [duty] => 0
        )

    [18] => stdClass Object
        (
            [action] => Buy
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.34068276
            [costs] => 0
            [duty] => 0
        )

    [19] => stdClass Object
        (
            [action] => Sell
            [date] => 2012-02-01
            [instrument] => USD/CAD
            [units] => 13.5
            [gbp_amount] => 0.34068276
            [costs] => 0
            [duty] => 0
        )

)

我希望实现的输出是这样的:

    Array
(
    [2011-02-14] => stdClass Object
            [Buy] => Array
                [0] = Array
                    (
                        [instrument] => USD/CAD
                        [units] => 2000.0
                        [gbp_amount] => 12.4869
                        [costs] => 0
                        [duty] => 0
                    )

            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 2000.0
                        [gbp_amount] => 14.62840335
                        [costs] => 0
                        [duty] => 0
                    )

    [2011-02-15] => stdClass Object
            [Buy] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 24.80064
                        [costs] => 0
                        [duty] => 0
                    )

            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 30.32498256
                        [costs] => 0
                        [duty] => 0
                    )

    [2011-02-21] => stdClass Object
            [Buy] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 24.63168
                        [costs] => 0
                        [duty] => 0
                    )
                [1] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 24.63168
                        [costs] => 0
                        [duty] => 0
                    )

            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 26.633004
                        [costs] => 0
                        [duty] => 0
                    )

    [2011-02-22] => stdClass Object
            [Buy] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 2000.0
                        [gbp_amount] => 12.31584
                        [costs] => 0
                        [duty] => 0
                    )

            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 4000.0
                        [gbp_amount] => 27.0270324
                        [costs] => 0
                        [duty] => 0
                    )

                [1] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 2000.0
                        [gbp_amount] => 13.88811768
                        [costs] => 0
                        [duty] => 0
                    )

    [2011-04-21] => stdClass Object
            [Buy] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 1000.0
                        [gbp_amount] => 12.09882
                        [costs] => 0
                        [duty] => 0
                    )

    [2011-04-22] => stdClass Object
            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 1000.0
                        [gbp_amount] => 13.87129713
                        [costs] => 0
                        [duty] => 0
                    )

    [2012-02-01] => stdClass Object
            [Buy] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.8517069
                        [costs] => 0
                        [duty] => 0
                    )

                [1] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 27.0
                        [gbp_amount] => 0.68136552
                        [costs] => 0
                        [duty] => 0
                    )

                [2] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.34068276
                        [costs] => 0
                        [duty] => 0
                    )

                [3] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.34068276
                        [costs] => 0
                        [duty] => 0
                    )

            [Sell] => Array
                [0] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.8517069
                        [costs] => 0
                        [duty] => 0
                    )

                [1] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 27.0
                        [gbp_amount] => 0.68136552
                        [costs] => 0
                        [duty] => 0
                    )

                [2] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.34068276
                        [costs] => 0
                        [duty] => 0
                    )

                [3] => Array
                    (
                        [instrument] => USD/CAD
                        [units] => 13.5
                        [gbp_amount] => 0.34068276
                        [costs] => 0
                        [duty] => 0
                    )

)

我尝试使用 foreach 循环重新排列数组,但除了让交易按日期列出之外,我几乎没有成功,但我无法根据买入/卖出列出这些。任何帮助将不胜感激。

4

1 回答 1

2

这应该这样做:

$res = array();
foreach ($items as $item) {
    $tmp = (array)$item;
    unset($tmp['date'], $tmp['action']);
    $res[$item->date][$item->action][] = $tmp;
}
print_r($res);

它将每个对象转换为一个数组,使用(然后删除)日期和操作字段作为结果数组的键,瞧 :)

于 2012-08-15T13:04:33.840 回答