提取重复项及其在php数组中出现的次数

提取重复项及其在php数组中出现的次数,php,arrays,Php,Arrays,我正在开发一个用户驱动的电子商务网站,需要一些帮助。我所拥有的是一个函数,它将在数组中循环删除重复项以及重复项出现的次数。然后,我需要在每个提取的重复项上运行一个函数,次数与它们出现的次数相同。到目前为止,我的代码可以工作,但是当有多个重复的代码具有相同的重复计数时,代码就会中断。这是我到目前为止所做的代码 $affiliates = array(11,11,12,12,13,13,13,14,14,14,14); //breaks the code $affiliates = array(11

我正在开发一个用户驱动的电子商务网站,需要一些帮助。我所拥有的是一个函数,它将在数组中循环删除重复项以及重复项出现的次数。然后,我需要在每个提取的重复项上运行一个函数,次数与它们出现的次数相同。到目前为止,我的代码可以工作,但是当有多个重复的代码具有相同的重复计数时,代码就会中断。这是我到目前为止所做的代码

$affiliates = array(11,11,12,12,13,13,13,14,14,14,14); //breaks the code
$affiliates = array(11,11,13,13,13,14,14,14,14,12,12,12,12,12); // works fine

$array = array();
$match_count = array();

  foreach($affiliates as $key => $affiliate) {
      $array[] = $affiliate;
  }

  arsort($array); // keeps array index in order

  foreach($array as $arrays) {
      if(array_value_count($arrays,$array) > 1) {   
          $match_count[] = array_value_count($arrays,$array);
      }
  }

  $match_count = array_unique($match_count);

  $array_unique = arrayDuplicate($array);

  $final_array = array_combine($match_count,$array_unique);

  foreach($final_array as $key => $value) {
      for($i = 0; $i < $key; $i++) {
          echo 'addOrder(affiliate_id = ' . $value . ') . '<br>';   
      }
  }
为了修复破坏代码的重复项,我尝试了以下方法

 if(count($array_unique) - count($match_count_unique) == 1 ) // do something

我如何知道在何处正确添加丢失的重复值计数和数组项,而不使它们失去同步?或者有更好的方法吗?


<?php
$affiliates = array(11,11,12,12,13,13,13,14,14,14,14); 

// get an array whose keys are the aff# and 
//the values are how many times they occur
$dupes = array();
foreach ($affiliates as $aff) {
  $dupes[$aff]++;
}
// remove the 1's since those aren't dupes
$dupes = preg_grep('~^1$~',$dupes,PREG_GREP_INVERT); 

// de-dupe the original array
$affiliates = array_unique($affiliates);

// for each duped affiliate...
foreach ($dupes as $aff => $affCount) {
  // for each time it was duped.. 
  for ($c=0;$c<$affCount;$c++) {
    // do something. $aff is the aff# like 11
  }
}
?>
取自


重复项=(数组大小)-(唯一值的总数)

我认为这会起作用,但我今晚才能检查它。直截了当地说,我不会删除数字1,而是将它们存储在另一个变量中,因为这些项仍然需要通过
addOrder()
函数运行。@handmdr好的,我有点好奇。。如果您必须通过
addOrder()
函数运行所有复制,甚至非复制。。为什么需要将它们分开,而不是仅仅在原始阵列中循环?我正在设计一个用户驱动的电子商务网站。上传到db的每个产品都有一个唯一的附属id。如果客户在一个订单上购买多个产品,则这些产品可能由多个用户拥有。我需要能够为每个分支机构创建单独的订单。例如,上面的数组将有5个订单,1个订单为分支机构11,其中2个产品,1个订单为分支机构12,其中2个产品,分支机构13,其中3个产品,分支机构14,其中4个产品。然后我需要为管理员制作一个主订单来跟踪所有销售。这可能是不太可能的,产品将在一个订单购买这个数量,但这是可能的,所以我需要计划它。我发誓我尝试了这个功能!!所以我想我写了所有这些代码都是白费力气的,因为它已经是一个本机函数了!!!!在您看来,哪种方法对大型阵列的性能更好?@handmdmr
array\u count\u value
的作用与我的解决方案中的
foreach
循环完全相同。我认为这两种方法都不会更有效率,因为它们基本上都是做同样的事情@KanishkaGanguly+1对于内置函数,尽管@handmdmr您仍然需要循环查看结果,正如我所示,因为您希望为每个复制的itemAFAIK做些事情,内置函数通常比我们自己编写的函数更优化。使用
array\u count\u values()
然后,我将使用迭代器计数的值和键循环遍历它们,以引用订单的附属id@蜡笔紫罗兰回答了你的问题,但忘了通知你,解释在你的代码下面。谢谢:-)
if(count($array_unique) != count($match_count_unique) == 1 ) // do something
<?php
$affiliates = array(11,11,12,12,13,13,13,14,14,14,14); 

// get an array whose keys are the aff# and 
//the values are how many times they occur
$dupes = array();
foreach ($affiliates as $aff) {
  $dupes[$aff]++;
}
// remove the 1's since those aren't dupes
$dupes = preg_grep('~^1$~',$dupes,PREG_GREP_INVERT); 

// de-dupe the original array
$affiliates = array_unique($affiliates);

// for each duped affiliate...
foreach ($dupes as $aff => $affCount) {
  // for each time it was duped.. 
  for ($c=0;$c<$affCount;$c++) {
    // do something. $aff is the aff# like 11
  }
}
?>
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);
No. of NON Duplicate Items: 7

Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)