How to merge subarrays by a single key value and count the duplicates?

纵然是瞬间 提交于 2019-12-13 09:32:08

问题


I'm new to stackoverflow and I need some help.

I'm trying to remove the duplicates from a multi-dimension array in PHP such as:

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

and create an array that removes the duplicates and adds to Quan the number of duplicates it as found like this (filtered by data) :

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

My code is the folowing: ($top is the array)

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

I've tried some answers that I found here on stack such as:

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

but I can't add how many there are to Quan.


回答1:


I found a solution myself with the help of other posts.

Here is a sample of my code:

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

if someone can help me make this code more beautifull I would be in your debt :)

thanks everyone tho!




回答2:


You will need to implement temporary keys so that as you iterate your array, you can determine if you are handling the first occurrence of Data or not. If not, you merely add the current Quan value to the first occurrence's Quan value.

When finished looping, you remove the temporary keys (reindex) by calling array_values().

Code: (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);

Output:

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

*Note if you want to ignore the Quan value and just add 1 each time, you can use:

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

Your question is ambiguous on this logic because of the Quan column data.



来源:https://stackoverflow.com/questions/32619813/how-to-merge-subarrays-by-a-single-key-value-and-count-the-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!