Php 对foreach中数组中的项进行分组和求和
我正在遍历两个存储过程的结果集,在一个存储过程中根据另一个存储过程中的字段获取结果 包含结果集的两个数组是Php 对foreach中数组中的项进行分组和求和,php,arrays,foreach,Php,Arrays,Foreach,我正在遍历两个存储过程的结果集,在一个存储过程中根据另一个存储过程中的字段获取结果 包含结果集的两个数组是$customers和$subcustomers foreach($customers as $customer) { foreach($subcustomers as $subcustomer) { if($subcustomer['parent'] == $customer['id']) {
$customers
和$subcustomers
foreach($customers as $customer)
{
foreach($subcustomers as $subcustomer)
{
if($subcustomer['parent'] == $customer['id'])
{
if($customer['innumber'] == null && $subcustomer['innumber'] != null)
{
$chartInboundSub['name'] = $customer['name'];
$chartInboundSub['label'] = $subcustomer['innumber'];
$chartInboundSub['countInbound'] = $customer['count'];
$chartInboundSub['minsInbound'] = ceil($customer['duration'] / 60);
$chartInboundSub['customerid'] = $customer['id'];
array_push($out['chartInbound'], $chartInboundSub);
}
}
}
}
print\r($out['chartInbound'])
的当前输出如下:
Array
(
[0] => Array
(
[countInbound] => 426
[minsInbound] => 340
[name] => Telekomm
[label] => 01-02
[customerid] => 6
)
[1] => Array
(
[countInbound] => 1
[minsInbound] => 2
[name] => Telekomm
[label] => 01-02
[customerid] => 6
)
[2] => Array
(
[countInbound] => 3
[minsInbound] => 21
[name] => Telekomm
[label] => 080
[customerid] => 6
)
[3] => Array
(
[countInbound] => 1920
[minsInbound] => 15766
[name] => Telekomm
[label] => 084
[customerid] => 6
)
[4] => Array
(
[countInbound] => 2332
[minsInbound] => 17521
[name] => Telekomm
[label] => 084
[customerid] => 6
)
...
)
上述结果需要按名称
、标签
、客户ID
与countInbound
和分钟Inbound
进行分组,因此:
所需输出应为:
Array
(
[0] => Array
(
[countInbound] => 427
[minsInbound] => 342
[name] => Telekomm
[label] => 01-02
[customerid] => 6
)
[1] => Array
(
[countInbound] => 3
[minsInbound] => 21
[name] => Telekomm
[label] => 080
[customerid] => 6
)
[2] => Array
(
[countInbound] => 4252
[minsInbound] => 33287
[name] => Telekomm
[label] => 084
[customerid] => 6
)
...
)
我认为这应该行得通。我还没有测试代码,所以我没有做出任何承诺
$map = array();
$i = 0;
foreach($customers as $customer)
{
foreach($subcustomers as $subcustomer)
{
if($subcustomer['parent'] == $customer['id'])
{
if($customer['innumber'] == null && $subcustomer['innumber'] != null)
{
$key = $customer['name'] . '/' . $subcustomer['innumber'] . '/' . $customer['id'];
if(isset($map[$key])) {
$out['chartInbound'][$map[$key]]['countInbound'] += $customer['count'];
$out['chartInbound'][$map[$key]]['minsInbound'] += ceil($customer['duration'] / 60);
}
else {
$out['chartInbound'][$i] = array(
'name' => $customer['name'],
'label' => $subcustomer['innumber'],
'countInbound' => $customer['count'],
'minsInbound' => ceil($customer['duration'] / 60),
'customerid' => $customer['id'],
);
$map[$key] = $i++;
}
}
}
}
}
对于名称
、标签
和客户ID
的每个组合,它都会创建一个字符串键,该键对于该组合必须是唯一的。然后检查该键是否已经有任何数据(通过在$out['chartInbound']
中保留一个单独的键及其索引列表)。如果是这样,它只添加countInbound
和mininbound
。如果没有,则将整个$chartInboundSub
放入$out['chartInbound']
请注意,这取决于密钥的唯一性。例如,如果在名称中允许
/
,则可能不是这样。而不是使用foreach
循环,我使用每个PHP数组都具有的“迭代器”
假设数组已排序,则记录“当前组id”的单次传递就足够了
我使用了一种“预读”技术,因此不需要“如果”测试来找出如何处理“当前记录”
守则:
/**
* Output stored in here...
*/
$output = array();
// groupId consists of: name, label, customerid
// read ahead - we need the current entry of the source array...
$currentEntry = current($source);
while ($currentEntry !== false) { // process the array / file / resultset etc.
// start of a group... process the first record that every group has...
$currentGroupId = getGroupId($currentEntry);
$currentGroupCountInbound = $currentEntry['countInbound'];
$currentGroupMinsInbound = $currentEntry['minsInbound'];
// read the next record as we always 'read ahead' after processing a record...
next($source);
$currentEntry = current($source);
while ($currentGroupId == getGroupId($currentEntry)) {
// same group = total the values...
$currentGroupCountInbound += $currentEntry['countInbound'];
$currentGroupMinsInbound += $currentEntry['minsInbound'];
// next entry in the input array - will end this group if not the same...
next($source);
$currentEntry = current($source);
}
// end of the current group -- output the information...
// add it to an array... or whatever...
$output[] = array('groupid' => $currentGroupId,
'countInbound' => $currentGroupCountInbound,
'minsInbound' => $currentGroupMinsInbound);
}
// show the output...
echo '<pre>';
print_r($output);
echo '</pre>';
exit;
// ---------------------------------
function getGroupId($entry = array())
{
if (empty($entry)) {
return array();
}
return array(
$entry['name'], $entry['label'], $entry['customerid']
);
}
对不起,我真的不明白,当名称、标签和customerid相同时,您想对
countInbound
和minInbound
求和吗?@PHPeter:是的,这是正确的
Array(
[0] => Array(
[groupid] => Array(
[0] => Telekomm
[1] => 01-02
[2] => 6
)
[countInbound] => 427
[minsInbound] => 342
)
[1] => Array(
[groupid] => Array(
[0] => Telekomm
[1] => 080
[2] => 6
)
[countInbound] => 3
[minsInbound] => 21
)
[2] => Array(
[groupid] => Array(
[0] => Telekomm
[1] => 084
[2] => 6
)
[countInbound] => 4252
[minsInbound] => 33287
)
)