我有一个具有以下结构的数组。有两个元素具有完全相同的日期/时间,差异是方向和值。
Array
(
[0] => Array
(
[date] => 2014-12-09
[time] => 21:58:15
[value] => 4
[in_out] => Outgoing
)
[1] => Array
(
[date] => 2014-12-09
[time] => 21:58:15
[value] => 5
[in_out] => Incoming
)
[2] => Array
(
[date] => 2014-12-09
[time] => 21:57:45
[value] => 5
[in_out] => Outgoing
)
[3] => Array
(
[date] => 2014-12-09
[time] => 21:57:45
[setup_name] => 5
[in_out] => Incoming
)
[4] => Array
(
[date] => 2014-12-09
[time] => 21:57:15
[setup_name] => 3
[in_out] => Incoming
)
[5] => Array
(
[date] => 2014-12-09
[time] => 21:57:15
[value] => 6
[in_out] => Outgoing
)
[6] => Array
(
[date] => 2014-12-09
[time] => 21:56:45
[value] => 6
[in_out] => Outgoing
)
[7] => Array
(
[date] => 2014-12-09
[time] => 21:56:45
[value] => 7
[in_out] => Incoming
)
[8] => Array
(
[date] => 2014-12-09
[time] => 21:56:15
[value] => 4
[in_out] => Outgoing
)
[9] => Array
(
[date] => 2014-12-09
[time] => 21:56:15
[value] => 5
[in_out] => Incoming
)
)
我需要将数据放入具有以下结构的新数组中:
Array
(
[0] => Array
(
[date] => 2014-12-09
[time] => 21:58:15
[out_value] => 4
[in_value] => 5
)
[1] => Array
(
[date] => 2014-12-09
[time] => 21:57:45
[out_value] => 5
[in_value] => 5
)
[2] => Array
(
[date] => 2014-12-09
[time] => 21:57:15
[out_value] => 6
[in_value] => 3
)
[3] => Array
(
[date] => 2014-12-09
[time] => 21:56:45
[out_value] => 6
[in_value] => 7
)
[4] => Array
(
[date] => 2014-12-09
[time] => 21:56:15
[out_value] => 4
[in_value] => 5
)
)
如果传出/传入一致地交替,但有时会有两个传入(3和4),这将是很简单的。
任何想法都会受到赞赏。
由于
答案 0 :(得分:0)
首先请让我确保在一秒钟内,只有一个Incoming
和一个Outgoing
?
如果你的答案是肯定的,请试试这个:
$input = array
(
'0' => array
(
'date' => '2014-12-09',
'time' => '21:58:15',
'value' => '4',
'in_out' => 'Outgoing'
),
'1' => array
(
'date' => '2014-12-09',
'time' => '21:58:15',
'value' => '5',
'in_out' => 'Incoming'
),
'2' => array
(
'date' => '2014-12-09',
'time' => '21:57:45',
'value' => '5',
'in_out' => 'Outgoing',
),
'3' => array
(
'date' => '2014-12-09',
'time' => '21:57:45',
'setup_name' => '5',
'in_out' => 'Incoming'
),
'4' => array
(
'date' => '2014-12-09',
'time' => '21:57:15',
'setup_name' => '3',
'in_out' => 'Incoming'
),
'5' => array
(
'date' => '2014-12-09',
'time' => '21:57:15',
'value' => '6',
'in_out' => 'Outgoing'
),
'6' => array
(
'date' => '2014-12-09',
'time' => '21:56:45',
'value' => '6',
'in_out' => 'Outgoing'
),
'7' => array
(
'date' => '2014-12-09',
'time' => '21:56:45',
'value' => '7',
'in_out' => 'Incoming'
),
'8' => array
(
'date' => '2014-12-09',
'time' => '21:56:15',
'value' => '4',
'in_out' => 'Outgoing'
),
'9' => array
(
'date' => '2014-12-09',
'time' => '21:56:15',
'value' => '5',
'in_out' => 'Incoming'
)
);
$temp = array();
$keys = array('Incoming' => 'in_value', 'Outgoing' => 'out_value');
foreach ($input as $line){
$temp[$line['date'].$line['time']]['date'] = $line['date'];
$temp[$line['date'].$line['time']]['time'] = $line['time'];
if(!empty($line['value'])){
$temp[$line['date'].$line['time']][$keys[$line['in_out']]] = $line['value'];
}elseif(!empty($line['setup_name'])){
$temp[$line['date'].$line['time']][$keys[$line['in_out']]] = $line['setup_name'];
}
}
$result = array_values($temp);
print_r($result);
答案 1 :(得分:0)
这是另一种方法:
$oldArrays = [
[ 'date' => '2014-12-09', 'time' => '21:58:15', 'value' => 4, 'in_out' => 'Outgoing' ],
[ 'date' => '2014-12-09', 'time' => '21:58:15', 'value' => 5, 'in_out' => 'Incoming' ],
[ 'date' => '2014-12-09', 'time' => '21:57:45', 'value' => 5, 'in_out' => 'Outgoing' ],
[ 'date' => '2014-12-09', 'time' => '21:57:45', 'value' => 5, 'in_out' => 'Incoming' ],
[ 'date' => '2014-12-09', 'time' => '21:57:15', 'value' => 3, 'in_out' => 'Incoming' ],
[ 'date' => '2014-12-09', 'time' => '21:57:15', 'value' => 6, 'in_out' => 'Outgoing' ]
// etc...
];
function array_combine_in_out($oldArrays)
{
$newArrays = [];
foreach ($oldArrays as $oldArray) {
$timestamp = $oldArray['date'] . ' ' . $oldArray['time'];
if (!array_key_exists($timestamp, $newArrays)) {
$newArrays[$timestamp] = [
'date' => $oldArray['date'],
'time' => $oldArray['time'],
'out_value' => null,
'in_value' => null
];
}
if ($oldArray['in_out'] == 'Outgoing') {
$newArrays[$timestamp]['out_value'] = $oldArray['value'];
}
if ($oldArray['in_out'] == 'Incoming') {
$newArrays[$timestamp]['in_value'] = $oldArray['value'];
}
}
return array_values($newArrays);
}
$newArrays = array_combine_in_out($oldArrays);