如何通过单个键值合并子数组并计算重复项?

时间:2015-09-16 22:28:38

标签: php arrays merge duplicates counting

我是stackoverflow的新手,我需要一些帮助。

我正在尝试从PHP中的多维数组中删除重复项,例如:

Array ( 
    [0] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 1 ) 
    [1] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 1 )
    [2] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 ) 
    [3] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 ) 
    [4] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 ) 
    [5] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 )
)

并创建一个删除重复项的数组,并将Quan重复项添加到此处(按数据过滤):

Array ( 
    [0] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 2 ) 
    [1] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 4 ) 
)

我的代码如下:( $top是数组)

foreach($top as $value){
    if(!empty($temp_top)){
        for($i =0;$i<sizeof($temp_top);$i++){
            if($value['Data'] == $temp_top[$i]['Data'] ){
                $temp_top[$i]['Quan'] +=1;
            }else{
                $temp_top[] = $value;
            }
        }
    }else{
        $temp_top[] = $value;
    }
}

我已经尝试了一些我在堆栈中找到的答案,例如:

$input = array_map("unserialize", array_unique(array_map("serialize", $top)));

但我无法添加Quan的数量。

2 个答案:

答案 0 :(得分:0)

我在其他帖子的帮助下找到了解决方案。

以下是我的代码示例:

$temp_top = array_map("unserialize", array_unique(array_map("serialize", $top)));
    $numvezesfound=0;

    foreach($temp_top as $key => $value){
            for($i =0;$i<sizeof($top);$i++){
                if($value['Data'] == $top[$i]['Data'] ){
                    $numvezesfound +=1;
                }
            }
            $temp_top[$key]['Quan'] = $numvezesfound;
            $numvezesfound = 0;
        }       
    $top = $temp_top;

如果有人可以帮我把这段代码变得更漂亮,我会欠你的债务:)。

谢谢大家!

答案 1 :(得分:0)

您需要实现临时密钥,以便在迭代数组时,可以确定是否正在处理第一次出现的Data。如果没有,您只需将当前Quan值添加到第一个匹配的Quan值。

完成循环后,您可以通过调用array_values()删除临时密钥(reindex)。

代码:(Demo

$array = [
    ["Plat" => "hello", "Data" => "01/01/2015", "Term" => "PHP", "Quan" => "1"],
    ["Plat" => "hello", "Data" => "01/01/2015", "Term" => "PHP", "Quan" => "1"],
    ["Plat" => "hello", "Data" => "03/01/2015", "Term" => "PHP", "Quan" => "1"],
    ["Plat" => "hello", "Data" => "03/01/2015", "Term" => "PHP", "Quan" => "1"],
    ["Plat" => "hello", "Data" => "03/01/2015", "Term" => "PHP", "Quan" => "1"],
    ["Plat" => "hello", "Data" => "03/01/2015", "Term" => "PHP", "Quan" => "1"],
];

foreach ($array as $row) {
    if (!isset($result[$row['Data']])) {
        $result[$row['Data']] = $row;
    } else {
        $result[$row['Data']]['Quan'] += $row['Quan'];
    }
}
$result = array_values($result);
var_export($result);

输出:

array (
  0 => 
  array (
    'Plat' => 'hello',
    'Data' => '01/01/2015',
    'Term' => 'PHP',
    'Quan' => 2,
  ),
  1 => 
  array (
    'Plat' => 'hello',
    'Data' => '03/01/2015',
    'Term' => 'PHP',
    'Quan' => 4,
  ),
)

*请注意,如果您想忽略Quan值并且每次只添加1,您可以使用:

if (!isset($result[$row['Data']])) {
    $result[$row['Data']]['Quan'] = 1;
} else {
    ++$result[$row['Data']]['Quan'];
}

由于Quan列数据,您的问题在此逻辑上不明确。