我试图根据相同的日期删除重复的数组值组(仅保留1组)。
同时取出" 1"中的数字。键(在pid键上方)并将其与重复的分组一起添加。
我所拥有的问题是每小时下的每个小组是"只有"假设在相同的"小时"下过滤重复的组。部分。
这里的数组im以:
开头Array(
[1488] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 37
[pid] => 1488
)
[1] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 51
[pid] => 1488
)
[2] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 44
[pid] => 1488
)
[3] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 22
[pid] => 1488
)
)
)
[2] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 9.5
[pid] => 2
)
[1] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 8
[pid] => 2
)
[2] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 7
[pid] => 2
)
[3] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 5
[pid] => 2
)
)
)
)
这就是我需要的样子:
Array(
[0] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 37
[pid] => 1488
)
[1] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 117
[pid] => 1488
)
)
)
[1] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 24.5
[pid] => 2
)
[1] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 5
[pid] => 2
)
)
)
)
这是我的代码:
$temp_array = array();
$w = 0;
$key_array = array();
$new_array = array();
foreach($project_hours as $old_arrs) {
foreach ($old_arrs["hours"] as $val) {
if (!in_array($val[0], $key_array)) {
$key_array[$w] = $val[0];
$temp_array[$w] = $val;
} else {
$ser = array_search($val[0], $key_array);
$temp_array[$ser][1] += $val[1];
}
$w++;
$old_arrs["hours"] = $temp_array;
}
$new_array[] = $old_arrs;
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
答案 0 :(得分:1)
您可以只使用一些简单的技巧来组织数组键,然后在两个区域中使用array_values()
将关联键重置为数字键:
// Your array
$data = array(
1488 => array(
'hours' => array(
0 => array(
0 => '2016/07/11 - 2016/07/17',
1 => 37,
'pid' => 1488,
),
1 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 51,
'pid' => 1488
),
2 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 44,
'pid' => 1488
),
3 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 22,
'pid' => 1488
)
)
),
2 => array(
'hours' => array(
0 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 9.5,
'pid' => 2
),
1 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 8,
'pid' => 2
),
2 => array(
0 => '2016/07/04 - 2016/07/10',
1 => 7,
'pid' => 2
),
3 => array(
0 => '2016/07/11 - 2016/07/17',
1 => 5,
'pid' => 2
)
)
)
);
// Loop through array...
foreach($data as $pid => $group) {
foreach($group['hours'] as $row) {
// I am using the date as a unique key so it's easy to organize
$new[$pid]['hours'][$row[0]][0] = $row[0];
// This will draw an warning if you don't first check that it's set
if(!isset($new[$pid]['hours'][$row[0]][1]))
$new[$pid]['hours'][$row[0]][1] = 0;
// Add values together as you go
$new[$pid]['hours'][$row[0]][1] += $row[1];
// On each loop this just assigns the pid (it just keeps overwriting itself
// but that is no big deal)
$new[$pid]['hours'][$row[0]]['pid'] = $pid;
}
// Here since you are not using date keys, just reset to numeric
$new[$pid]['hours'] = array_values($new[$pid]['hours']);
}
print_r(array_values($new));
给你:
Array
(
[0] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 37
[pid] => 1488
)
[1] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 117
[pid] => 1488
)
)
)
[1] => Array
(
[hours] => Array
(
[0] => Array
(
[0] => 2016/07/04 - 2016/07/10
[1] => 24.5
[pid] => 2
)
[1] => Array
(
[0] => 2016/07/11 - 2016/07/17
[1] => 5
[pid] => 2
)
)
)
)