我有一系列包含买卖交易的数据。我试图根据日期对交易进行分组,然后在他们自己的组内合并买入/卖出。我的输入如下:
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循环来重新安排阵列,但除了让交易按日期列出之外我没有什么成功,但我无法根据买/卖来列出这些。任何帮助都会很感激。
答案 0 :(得分:2)
这应该这样做:
$res = array();
foreach ($items as $item) {
$tmp = (array)$item;
unset($tmp['date'], $tmp['action']);
$res[$item->date][$item->action][] = $tmp;
}
print_r($res);
它将每个对象转换为一个数组,使用(然后删除)日期和操作字段作为结果数组的键,并且瞧瞧:)