Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将重复数组与计数合并_Php_Arrays_Laravel 5.1_Array Merge - Fatal编程技术网

Php 将重复数组与计数合并

Php 将重复数组与计数合并,php,arrays,laravel-5.1,array-merge,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 "sta

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

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,如果有必要,我可以将数组转换为集合,这样就可以使用辅助函数了

我自己设法用foreach循环修复了它,它的运行速度比我预期的快(0.3179秒)

我在反弹中循环,如果合并为空(第一次迭代),它只添加硬反弹。 从那时起,我将循环新数组并检查是否存在重复。当这种情况发生时,我们只需在数量上增加一个,然后从foreach中分离出来。否则,它最终仍将添加重复项。 在我检查它是否进入最后一次迭代之后,这意味着如果它仍然没有中断,我们应该添加硬反弹,因为它还不存在


为了说明这一点,我确保在循环运行之前,数量1已经存在,而不是在循环过程中添加它。

我自己设法用foreach循环修复它,它的运行速度比我预期的快(0.3179秒)

我在反弹中循环,如果合并为空(第一次迭代),它只添加硬反弹。 从那时起,我将循环新数组并检查是否存在重复。当这种情况发生时,我们只需在数量上增加一个,然后从foreach中分离出来。否则,它最终仍将添加重复项。 在我检查它是否进入最后一次迭代之后,这意味着如果它仍然没有中断,我们应该添加硬反弹,因为它还不存在


为了明确起见,我确保在循环运行之前数量1已经存在,而不是在循环期间添加它。

我不确定这段代码对很多项的效率有多高(顺便说一句,您没有提到数字的数量级),但我认为它比将数组转换为Laravel集合更有效

$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);

我不确定这段代码在处理很多项时有多高效(顺便说一句,您没有提到数字的数量级),但我认为它比将数组转换为Laravel集合更高效

$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);
试一试


试试看

<?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";


    ?>



有趣的问题。您是否尝试过
array\u count\u values()
?您当前是否正在从MySql之类的东西收集这些数据集?是的,请使用雄辩的方式获取这些值。为什么不使用然后聚合SQL函数进行计数?我不太擅长子查询和所有有趣的问题。您是否尝试过
array\u count\u values()
?您当前是否正在从MySql之类的工具收集这些数据集?是的,请使用雄辩的方法获取这些值。为什么不使用然后聚合SQL函数进行计数?我不太擅长子查询和所有这些东西这仍然会给我3721个结果。除非我以某种方式拥有完整的数据集,否则很难进行调试。你看到你提供的样本是如何工作的了吗?是的,我看到了,我只是用8个结果运行了它。这仍然不是它应该看起来的样子。它将域名设置为一个键,我认为这是不必要的,在该数组中,它为计数添加了另一个数组,该数组应该是一个带值的键。我自己解决了这个问题,看看我给出的答案,看看你在做什么,为什么要将域名添加为键,看看它是否已经存在,这并不意味着它是独一无二的。还应考虑状态和代码。我应该把我的案子做得更好。SorryThis仍然给我3721个结果。除非我有完整的数据集,否则很难调试。你看到你提供的样本是如何工作的了吗?是的,我看到了,我只是用8个结果运行了它。这仍然不是它应该看起来的样子。它将域名设置为一个键,我认为这是不必要的,在该数组中,它为计数添加了另一个数组,该数组应该是一个带值的键。我自己解决了这个问题,看看我给出的答案,看看你在做什么,为什么要将域名添加为键,看看它是否已经存在,这并不意味着它是独一无二的。还应考虑状态和代码。我应该把我的案子做得更好。抱歉,这仍然输出完整的结果集吗?仍然得到3721个结果,看看我自己提供的答案。这仍然输出完整的结果集?仍然得到3721个结果,看看我自己提供的答案。