从内部的foreach阵列组和项目总和 [英] Group and sum items from array within foreach

查看:91
本文介绍了从内部的foreach阵列组和项目总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据其他领域内通过循环的结果集的两个存储过程,一是存储过程中得到结果。

包含结果两个数组集 $客户 $ subcustomers

 的foreach($客户为$客户)
{
      的foreach($ subcustomers为$ subcustomer)
      {
            如果($ subcustomer ['父'] == $顾客['身份证'])
            {
                  如果($顾客['innumber'] == NULL和放大器;&安培;!$ subcustomer ['innumber'] = NULL)
                  {
                      $ chartInboundSub ['名'] = $顾客['名'];
                      $ chartInboundSub ['标签'] = $ subcustomer ['innumber'];
                      $ chartInboundSub ['countInbound'] = $顾客['伯爵'];
                      $ chartInboundSub ['minsInbound'] = CEIL($顾客['时间'] / 60);
                      $ chartInboundSub ['CUSTOMERID'] = $顾客['身份证'];                      array_push($出['chartInbound'],$ chartInboundSub);
                  }
            }
       }
}

的电流输出的print_r($出['chartInbound'])是如下:

 阵列

    [0] =>排列
        (
            [countInbound] => 426
            [minsInbound] => 340
            [名] => Telekomm
            [标签] => 01-02
            [客户] => 6
        )    [1] =>排列
        (
            [countInbound] => 1
            [minsInbound] => 2
            [名] => Telekomm
            [标签] => 01-02
            [客户] => 6
        )    [2] =>排列
        (
            [countInbound] => 3
            [minsInbound] => 21
            [名] => Telekomm
            [标签] => 080
            [客户] => 6
        )    [3] =>排列
        (
            [countInbound] => 1920
            [minsInbound] => 15766
            [名] => Telekomm
            [标签] => 084
            [客户] => 6
        )    [4] =>排列
        (
            [countInbound] => 2332
            [minsInbound] => 17521
            [名] => Telekomm
            [标签] => 084
            [客户] => 6
        )
    ...

以上结果需要由名称标签客户ID countInbound minsInbound 总结,这样:

所需的输出应为:

 阵列
    (
        [0] =>排列
            (
                [countInbound] => 427
                [minsInbound] => 342
                [名] => Telekomm
                [标签] => 01-02
                [客户] => 6
            )        [1] =>排列
            (
                [countInbound] => 3
                [minsInbound] => 21
                [名] => Telekomm
                [标签] => 080
                [客户] => 6
            )        [2] =>排列
            (
                [countInbound] => 4252
                [minsInbound] => 33287
                [名] => Telekomm
                [标签] => 084
                [客户] => 6
            )
        ...
    )


解决方案

我认为这应该工作。我还没有测试code,所以我做任何承诺。

  $ =图阵列();
$ I = 0;的foreach($客户为$客户)
{
      的foreach($ subcustomers为$ subcustomer)
      {
            如果($ subcustomer ['父'] == $顾客['身份证'])
            {
                  如果($顾客['innumber'] == NULL和放大器;&安培;!$ subcustomer ['innumber'] = NULL)
                  {                      $关键= $顾客['名']。 '/'。 $ subcustomer ['innumber']。 '/'。 $顾客['身份证'];                      如果(使用isset($图[$关键])){
                            $出['chartInbound'] [$图[$关键] ['countInbound'] + = $顾客['伯爵'];
                            $出['chartInbound'] [$图[$关键] ['minsInbound'] + = CEIL($顾客['时间'] / 60);
                      }
                      其他{
                          $出['chartInbound'] [$ i] =阵列(
                                  '名'=> $顾客[名称]
                                  标签= GT; $ subcustomer ['innumber'],
                                  countInbound'=> $顾客['伯爵'],
                                  minsInbound'=> CEIL($顾客['时间'] / 60)
                                  CUSTOMERID'=> $顾客['身份证'],
                          );
                          $图[$关键] = $ I ++;
                    }
                  }
            }
       }
}

有关名称标签客户ID 这将创建具有成为该组合唯一的字符串键。然后,它会检查是否已经有该键的任何数据(通过保持键和它们的索引的单独列表中的 $出['chartInbound'] )。如果是这样,它只是增加了 countInbound minsInbound 。如果不是把整个 $ chartInboundSub $出['chartInbound']

请注意,这依赖于键为唯一的。如果你比如让 / 中可能不会是这样的名字。

I'm looping through the result sets of two stored procedures, getting results within one stored procedures based on fields within the other.

The two arrays containing the results sets are $customers and $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);
                  }
            }
       }
}

The current output of print_r($out['chartInbound']) is the below:

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
        )
    ...
)

The above results need to be grouped by name, label, customerid with countInbound and minsInbound summed, so :

The desired output should be:

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
            )
        ...
    )

解决方案

I think this should work. I haven't tested the code, so I make no promises.

$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++;
                    }
                  }
            }
       }
}

For every combination of name, label and customerid it creates a string key that has to be unique for that combination. Then it checks if there is already any data for that key (by keeping a separate list of keys and their indexes in $out['chartInbound']). If so it just adds the countInbound and minsInbound. If not it puts the whole $chartInboundSub into $out['chartInbound'].

Please note that this relies on the key being unique. If you for instance allow / in the names that might not be the case.

这篇关于从内部的foreach阵列组和项目总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆