在关联数组中查找重复值,并将其添加到计数中 [英] Look for duplicate values in a associative array and add them to a count

查看:111
本文介绍了在关联数组中查找重复值,并将其添加到计数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算如下关联数组中重复值的数量:

Hi I am trying to count the number of duplicate values in a associative array that looks like this:

array(3) { [0]=> array(3) { ["Title"]=> string(25) "hello" 
                            ["Price"]=> int(50) 
                            ["Count"]=> int(1) }
           [1]=> array(3) { ["Title"]=> string(35) "world" 
                            ["Price"]=> int(50) 
                            ["Count"]=> int(1) } 
           [2]=> array(3) { ["Title"]=> string(25) "hello" 
                            ["Price"]=> int(50) 
                            ["Count"]=> int(1) } } 

如您所见,在标题"标签中有一个重复的值,我想对它们进行计数并将一个值添加到计数"部分.我开始做这样的事情:

As you can see here there is a duplicate value in the "Title" lable I want to count them and add one to the "Count" part. I started to do something like this:

$prodArray = array();

// Add the values to the array if it's the first time it occurs.
if (!in_array($row['prodTitle'], $prodArray["Title"]))
{
array_push($prodArray, 
           array( Title => $row['prodTitle'],
                  Price => $row['prodPrice'],
                  Count => 1)
          );
}
else
{
//Add one to the count for the current item.
}   

问题是我无法通过in_array函数访问数组中的"Title"元素.欢迎任何建议.

the thing is I can't access the "Title" element in the array through the in_array function. Any suggestions are welcome.

推荐答案

如果要检测正在创建的数组中的重复项,则可以避免多次遍历该数组:

If you want to detect dups in an array that you are creating, something like this avoid having to go through the array multiple times:

$arr=array();
while($row = mysql_fetch_array($result))  {                  
  $arr[$row['prodTitle']] = isset($arr[$row['prodTitle']]) 
                               ? $arr[$row['prodTitle']] +1 
                               : 0;                    
}
$dups = array_keys(array_filter($arr)); //any key => 0 will be filtred out

如果您只想通过SQL直接获取dups,请查看以下内容:

If you want to just get the dups directly by SQL, have a look at this:

您当前的查询-

SELECT prodTitle 
  FROM product 
WHERE prodTitle != '{$keyword}' 
  AND creditCard IN( SELECT creditCard FROM product WHERE prodTitle ='{$keyword}');

给出了这样的数据

prod cc
A    1
A    2
A    3
A    1
A    1
B    15
B    1
B    2
B    21
C    10
C    1

返回此集合(带有$ keyword =='A'):

returns this set (with $keyword=='A'):

prod 
B
B
C

仅返回 记录的汇总查询,其中在非X上使用的信用卡也至少在X上使用过两次-

An aggregate query that returns only records where credit cards used on non-X were also used on X at least twice --

SELECT p1.prodTitle, COUNT(p2.creditCard) AS numrecords
  FROM product p1
    JOIN product p2
    ON (p1.creditCard = p2.creditCard)
WHERE p1.prodTitle != '{$keyword}'
  AND p2.prodTitle = '{$keyword}'
GROUP BY p1.prodTitle 
  HAVING COUNT(p2.creditCard) > 1;

给定相同的数据,返回此集合:

given the same data, returns this set:

prod  num
B    2

执行聚合查询可以避免所有关于循环的混乱.这是链接到MySQL聚合列表功能.

Doing an aggregate query avoids all the messing about with loops. Here's a link to the MySQL list of aggregate functions.

这篇关于在关联数组中查找重复值,并将其添加到计数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆