如何通过单个键值合并子数组并计算重复项? [英] How to merge subarrays by a single key value and count the duplicates?
问题描述
我是Stackoverflow的新手,我需要一些帮助.
I'm new to stackoverflow and I need some help.
我正在尝试从PHP中的多维数组中删除重复项,例如:
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 )
)
并创建一个数组,该数组删除重复项,并将重复项的数量添加到Quan
,如下所示(按数据过滤):
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 )
)
我的代码如下:($top
是数组)
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)));
但是我不能在Quan
中添加多少.
but I can't add how many there are to Quan
.
推荐答案
您将需要实现临时键,以便在迭代数组时可以确定是否要处理Data
的第一次出现.如果不是,则只需将当前的Quan
值添加到第一次出现的Quan
值.
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.
完成循环后,可以通过调用array_values()
删除临时键(重新索引).
When finished looping, you remove the temporary keys (reindex) by calling array_values()
.
代码:(演示)
$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
,则可以使用:
*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'];
}
由于Quan
列数据,您的问题在此逻辑上不明确.
Your question is ambiguous on this logic because of the Quan
column data.
这篇关于如何通过单个键值合并子数组并计算重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!