合并具有计数

时间:2015-12-10 12:49:53

标签: php arrays laravel-5.1 array-merge

我正在处理大量数据,我需要在重复时合并数组。如果它们合并,我需要在阵列中添加一个计数。

array:3721 [▼
  0 => array:3 [▼
    "subscriber" => "gmail.com."
    "code" => 554
    "status" => 50
  ]
  1 => array:3 [▼
    "subscriber" => "apied.be"
    "code" => 550
    "status" => 51
  ]
  2 => array:3 [▼
    "subscriber" => "beton-dobbelaere.be"
    "code" => 550
    "status" => 50
  ]
  3 => array:3 [▼
    "subscriber" => "live.be"
    "code" => 550
    "status" => 51
  ]
  4 => array:3 [▼
    "subscriber" => "hotmail.be"
    "code" => 550
    "status" => 51
  ]
  5 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 50
  ]
  6 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 55
  ]
  7 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 51
  ]
  8 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 51
  ]

这应该类似于:

array:3721 [▼
  0 => array:3 [▼
    "subscriber" => "gmail.com."
    "code" => 554
    "status" => 50
    "amount" => 1
  ]
  1 => array:3 [▼
    "subscriber" => "apied.be"
    "code" => 550
    "status" => 51
    "amount" => 1
  ]
  2 => array:3 [▼
    "subscriber" => "beton-dobbelaere.be"
    "code" => 550
    "status" => 50
    "amount" => 1
  ]
  3 => array:3 [▼
    "subscriber" => "live.be"
    "code" => 550
    "status" => 51
    "amount" => 1
  ]
  4 => array:3 [▼
    "subscriber" => "hotmail.be"
    "code" => 550
    "status" => 51
    "amount" => 1
  ]
  5 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 50
    "amount" => 1
  ]
  6 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 55
    "amount" => 1
  ]
  7 => array:3 [▼
    "subscriber" => "telenet.be"
    "code" => 550
    "status" => 51
    "amount" => 2
  ]

当我使用

合并此示例时
array_unique($hardbounces, SORT_REGULAR);

我留下了大约534个结果,而不是3721,这很好,只是我需要知道数量,它必须有点效率,因为结果集可能非常大(更大)。

在需要对域和金额进行排序之后。

我正在使用laravel 5.1,如果有必要,我可以将数组转换为集合,因此可以使用辅助函数

3 个答案:

答案 0 :(得分:0)

我设法使用foreach循环自行修复它比我预期的更快(0.3179秒)

   $merged = [];
    foreach($hardbounces as &$hardbounce){
        if(empty($merged)){
            $merged[] = $hardbounce;
        }else{
            $i = 0;
            foreach($merged as $key => $merge){
                $i++;
                if($hardbounce['subscriber'] == $merge['subscriber'] && $hardbounce['code'] == $merge['code'] && $hardbounce['status'] == $merge['status']){
                    $merged[$key]['amount']++;
                    break;
                }
                if(count($merged) == $i){
                    $merged[] = $hardbounce;
                }
            }
        }
    }

我循环跳过,如果合并为空(第一次迭代),它只是添加硬弹跳。 从那时起,我将遍历新数组并检查是否存在重复。当发生这种情况时,我们只需添加一个金额并打破foreach。否则它最终仍会添加重复项。 在我检查我们是否进入最后一次迭代后,这意味着如果它仍然没有破坏,我们应该添加硬弹跳,因为它还不存在。

要清楚,我确保在此循环运行之前已存在金额1,而不是在此循环期间添加它。

答案 1 :(得分:0)

我不确定这段代码对于很多项目有多高效(BTW,你没有提到数字的数量级),但我认为它比将数组转换成Laravel集合更有效率< / p>

$arr = [
      0 =>  [
        "subscriber" => "gmail.com.",
        "code" => 554,
        "status" => 50,
      ],
      1 =>  [
        "subscriber" => "apied.be",
        "code" => 550,
        "status" => 51,
      ],
      2 =>  [
        "subscriber" => "beton-dobbelaere.be",
        "code" => 550,
        "status" => 50,
      ],
      3 =>  [
        "subscriber" => "live.be",
        "code" => 550,
        "status" => 51,
      ],
      4 =>  [
        "subscriber" => "hotmail.be",
        "code" => 550,
        "status" => 51,
      ],
      5 =>  [
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 50,
      ],
      6 =>  [
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 55,
      ],
      7 =>  [
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 51,
      ],
      8 =>  [
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 51,
      ],
];

$res = [];
foreach($arr as $element) {
    if(empty($res[$element['subscriber']])) {
        $res[$element['subscriber']] = [$element, 'count' => 1];
    } else {
        $res[$element['subscriber']]['count']++;
    }
}

var_dump($res);

答案 2 :(得分:0)

尝试

<?php
    $input = array(
      0 => array(
        "subscriber" => "gmail.com.",
        "code" => 554,
        "status" => 50),
      1 => array(
        "subscriber" => "apied.be",
        "code" => 550,
        "status" => 51),
      2 => array(
        "subscriber" => "beton-dobbelaere.be",
        "code" => 550,
        "status" => 50),
      3 => array(
        "subscriber" => "live.be",
        "code" => 550,
        "status" => 51),
      4 => array(
        "subscriber" => "hotmail.be",
        "code" => 550,
        "status" => 51),
      5 => array(
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 50),
      6 => array(
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 55),
      7 => array(
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 51),
      8 => array(
        "subscriber" => "telenet.be",
        "code" => 550,
        "status" => 51)
    );


    /**
     *@param array $counted The array already counted or NULL
     *@param array $new The array to count or to merge with the counted $counted
     */
    function merge_xor_count(array $counted = NULL, array $new){
        if($counted === NULL){
            $counted = array();
        }
        foreach($new as $keyNew => $valueNew){
            $matches = false;
            foreach($counted as $keyOut => $valueOut){
                if ($valueOut['subscriber'] == $valueNew['subscriber'] && $valueOut['code'] == $valueNew['code'] &&
                    $valueOut['status'] == $valueNew['status']){
                    $matches = $keyOut;
                }
            }
            if($matches !== false){
                $counted[$matches]['amount']++;
            }
            else{
                if(!isset($valueNew['amount'])) $valueNew['amount'] = 1;
                $counted[] = $valueNew;
            }
        }
        return $counted;
    }

    $output = merge_xor_count(NULL, $input);

    print_r ($output)."\n";

    $output = merge_xor_count($output, $input);

    print_r ($output)."\n";


    ?>