你好我试图计算一个关联数组,看起来像这样重复值的数量:查找重复的值,并将它们添加到数
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_array函数访问数组中的“Title”元素。欢迎任何建议。
对它们进行排序,然后运行一个foreach循环,比较一个与前者,并在每次相等时向计数器加1? – iaintunderstand 2012-08-12 20:39:09
你有没有一个看起来如何的例子? – user1593846 2012-08-12 22:11:34
嗯,我不是专家,但你知道有这样做的功能,没有必要发明任何东西。这里是这些函数的链接http://php.net/manual/en/array.sorting.php – iaintunderstand 2012-08-13 06:03:25