Php 在关联数组中查找重复值并将其添加到计数中
您好,我正在尝试计算关联数组中重复值的数量,如下所示:Php 在关联数组中查找重复值并将其添加到计数中,php,arrays,duplicates,associative,Php,Arrays,Duplicates,Associative,您好,我正在尝试计算关联数组中重复值的数量,如下所示: array(3) { [0]=> array(3) { ["Title"]=> string(25) "hello" ["Price"]=> int(50) ["Count"]=> int(1) } [1]=> array(3) { ["Title"]=> string
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) } }
$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.
}
正如您在这里看到的,在标题标签中有一个重复的值,我想对它们进行计数,并将其添加到计数部分。我开始这样做:
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) } }
$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_数组函数访问数组中的Title元素。欢迎任何建议。好的,我找到了我的解决方案,我让它看起来像这样,可能不是最有效的方法,但它是有效的:
$prodArray = array();
$ar = array();
$query ="THE QUERY";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
array_push($ar, $row['prodTitle']);
}
function findDuplicates($data,$dupval) {
$nb= 0;
foreach($data as $key => $val)
if ($val==$dupval) $nb++;
return $nb;
}
$uniarr = array_unique($ar);
//Will run the function findDuplicates for each unique title in the array
for ($i = 0; $i < sizeof($uniarr); $i++)
{
$count = findDuplicates($ar, $uniarr[$i]);
array_push($prodArray, array( Title => $uniarr[$i], Count => $count));
}
//Displays 2 of the results in the array
for ($c = 0; $c < 2; $c++)
{
echo "The title:".$prodArray[$c]["Title"]." And the amount:".$prodArray[$c]["Count"];
echo "<br />";
}
因此,您可以随意发表您的评论,如果您有任何改进,也可以随意发表。如果您想检测正在创建的阵列中的DUP,可以这样做,以避免多次检查阵列:
$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直接获取DUP,请查看以下内容:
您当前的查询-
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':
prod
B
B
C
一种聚合查询,仅返回非X上使用的信用卡在X上至少使用了两次的记录-
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;
给定相同的数据,返回此集合:
prod num
B 2
执行聚合查询可以避免所有循环的混乱。下面是MySQL聚合函数列表的一个示例 对它们进行排序,然后运行foreach循环,将一个循环与前一个循环进行比较,并在每次相等时向计数器中添加1?你有没有这样的例子?我一点都不是专家,但你知道有些函数可以做到这一点,没有必要发明任何东西。这里是这些函数的链接,我已经浏览了其中的一些函数,我发现了一些可能有用的东西,它看起来像这个链接中的第一个示例,我只是不完全理解代码,所以如果有人能为我解释这个函数,我将不胜感激。既然您正在执行查询,为什么不在查询中查找重复项,而不是在生成的数组中查找它们呢?我不太擅长SQL,因此我不确定如何计算重复项的数量,并在现有查询中向数组添加计数和标题。查询如下所示:$query=从product中选择prodTitle,其中prodTitle!='{$keyword}'和creditCard在从产品中选择creditCard,其中prodTitle='{$keyword}';非常感谢这个解决方案,这个查询正是我所需要的,竖起大拇指。