我有多个数组值的数组我希望在现有数组值中形成新数组,如果绘制类型相同则返回单个数组并计算相同绘图时间的时间。
Actual Array:
(
[0] => Array
(
[Time] => 00:45
[PlottingType] => Research
)
[1] => Array
(
[Time] => 00:45
[PlottingType] => admin
)
[2] => Array
(
[Time] => 00:45
[PlottingType] => admin
)
[3] => Array
(
[Time] => 00:45
[PlottingType] => Research
)
[4] => Array
(
[Time] => 00:30
[PlottingType] => Research
)
)
Expected Array:
Array
(
[0] => Array
(
[Time] => 01:30
[PlottingType] => Research
)
[1] => Array
(
[Time] => 01:30
[PlottingType] => admin
)
)
将相同的绘图类型时间计算为单个索引到单个数组中。所以请建议适当的解决方案。
答案 0 :(得分:0)
看起来您正在尝试删除数组中的重复项。在这种情况下,使用array_unique()
函数。
$expectedarray = array_unique($actualarray);
print_r($expectedarray);
如果要计算值,请使用array_count_values()
intead array_unique()
有关详细信息,请参阅此处:http://php.net/manual/en/function.array-unique.php
答案 1 :(得分:0)
$expectedarray = array();
foreach($ar as $yourmainarray)
{
foreach($u as $expectedarray)
{
if($ar['PlottingType'] == $u['PlottingType'])
{
$u['Time'] = $u['Time'] + $ar['Time'];
}
else
{
array_push($ar, $expectedarray);
}
}
}
它将提供这样的输出
[0] => Array
(
[Time] => 2:00
[PlottingType] => Research
)
[1] => Array
(
[Time] => 01:30
[PlottingType] => admin
)
您可以将其检查为
foreach($u as $expectedarray)
{
print_r($u);
}
答案 2 :(得分:0)
$data = array(
array('Time' => '00:45', 'PlottingType' => 'Research'),
array('Time' => '00:45', 'PlottingType' => 'admin'),
array('Time' => '00:45', 'PlottingType' => 'admin'),
array('Time' => '00:45', 'PlottingType' => 'Research'),
array('Time' => '00:30', 'PlottingType' => 'Research'),
);
print_r(array_reduce($data, function($result, $item) {
$key = $item['PlottingType'];
if (!isset($result[$key]))
$result[$key] = $item;
else
{
list($h1, $m1) = explode(':', $result[$key]['Time']);
list($h2, $m2) = explode(':', $item['Time']);
$time = ($h1 + $h2) * 60 + $m1 + $m2;
$h = floor($time / 60);
$m = $time % 60;
$result[$key]['Time'] = str_pad($h, 2, "0", STR_PAD_LEFT) . ':' .
str_pad($m, 2, "0", STR_PAD_LEFT);
}
return $result;
}, array()));
可以使用strtotime
和date
,但我不想这样做,因此代码会稍长一些。如果要从结果中删除最终键 - 将函数array_values
应用于它。
结果
Array
(
[Research] => Array
(
[Time] => 02:00
[PlottingType] => Research
)
[admin] => Array
(
[Time] => 01:30
[PlottingType] => admin
)
)
ps:和另一种方式
print_r(array_reduce($data, function($result, $item) {
$key = $item['PlottingType'];
if (!isset($result[$key]))
$result[$key] = $item;
else
{
$time = strtotime('0:' . $result[$key]['Time']);
list($h, $m) = explode(':', $item['Time']);
$time = strtotime('+' . $h . ' minutes', $time);
$time = strtotime('+' . $m . ' seconds', $time);
$result[$key]['Time'] = date('i:s', $time);
}
return $result;
}, array()));
这里我用分钟作为秒和小时作为分钟,所以答案可能超过24小时,最多60分钟。
pps:好的,你不能自己把它分配给变量,我会把它写下来
$data = array_values(array_reduce($data, function($result, $item) {
$key = $item['PlottingType'];
if (!isset($result[$key]))
$result[$key] = $item;
else
{
$time = strtotime('0:' . $result[$key]['Time']);
list($h, $m) = explode(':', $item['Time']);
$time = strtotime('+' . $h . ' minutes', $time);
$time = strtotime('+' . $m . ' seconds', $time);
$result[$key]['Time'] = date('i:s', $time);
}
return $result;
}, array()));
答案 3 :(得分:0)
确定。我正在删除旧的答案,这是新的工作。我希望你能做一些必要的小修改。
function add_minutes($time_1, $time_2) {
$temp = explode(':', $time_1);
$hr_1 = $temp[0]; $min_1 = $temp[1];
$temp = explode(':', $time_2);
$hr_2 = $temp[0]; $min_2 = $temp[1];
$min_2 = $min_2+$min_1;
$min_1 = intval($min_2/60);
$hr_2 = $hr_2 + $hr_1;
$hr_2 += $min_1;
$min_2 = $min_2%60;
$min_2 = sprintf('%02d', $min_2); $hr_2 = sprintf('%02d', $hr_2);
return $hr_2.':'.$min_2;
}
$actual_array = array();
$actual_array[] = array('Time' => '01:45', 'PlottingType' => 'Research');
$actual_array[] = array('Time' => '00:45', 'PlottingType' => 'admin');
$actual_array[] = array('Time' => '00:45', 'PlottingType' => 'admin');
$actual_array[] = array('Time' => '00:45', 'PlottingType' => 'Research');
$actual_array[] = array('Time' => '00:45', 'PlottingType' => 'Research');
$single_array = array();
foreach($actual_array as $val) {
if (key_exists($val['PlottingType'], $single_array)) {
$single_array[$val['PlottingType']] = add_minutes($val['Time'], $single_array[$val['PlottingType']]);
} else {
$single_array[$val['PlottingType']] = $val['Time'];
}
}
$output_array = array();
foreach ($single_array as $key => $value) {
$output_array[] = array('PlottingType' => $key, 'Time' => $value);
}
// $output_array this is your answer.
echo '<pre>'; var_dump($output_array);