• <tfoot id='xPoba'></tfoot>
    • <bdo id='xPoba'></bdo><ul id='xPoba'></ul>
      <i id='xPoba'><tr id='xPoba'><dt id='xPoba'><q id='xPoba'><span id='xPoba'><b id='xPoba'><form id='xPoba'><ins id='xPoba'></ins><ul id='xPoba'></ul><sub id='xPoba'></sub></form><legend id='xPoba'></legend><bdo id='xPoba'><pre id='xPoba'><center id='xPoba'></center></pre></bdo></b><th id='xPoba'></th></span></q></dt></tr></i><div id='xPoba'><tfoot id='xPoba'></tfoot><dl id='xPoba'><fieldset id='xPoba'></fieldset></dl></div>
      <legend id='xPoba'><style id='xPoba'><dir id='xPoba'><q id='xPoba'></q></dir></style></legend>

      <small id='xPoba'></small><noframes id='xPoba'>

        PHP:如何识别和更改数组中的重复值?

        时间:2023-11-30
          <i id='fws2W'><tr id='fws2W'><dt id='fws2W'><q id='fws2W'><span id='fws2W'><b id='fws2W'><form id='fws2W'><ins id='fws2W'></ins><ul id='fws2W'></ul><sub id='fws2W'></sub></form><legend id='fws2W'></legend><bdo id='fws2W'><pre id='fws2W'><center id='fws2W'></center></pre></bdo></b><th id='fws2W'></th></span></q></dt></tr></i><div id='fws2W'><tfoot id='fws2W'></tfoot><dl id='fws2W'><fieldset id='fws2W'></fieldset></dl></div>

            • <bdo id='fws2W'></bdo><ul id='fws2W'></ul>

            • <small id='fws2W'></small><noframes id='fws2W'>

                • <legend id='fws2W'><style id='fws2W'><dir id='fws2W'><q id='fws2W'></q></dir></style></legend>

                    <tbody id='fws2W'></tbody>

                  <tfoot id='fws2W'></tfoot>
                  本文介绍了PHP:如何识别和更改数组中的重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  好的,在 php 数组中有很多重复检测和删除的例子,使用 array_unique() 等但是如果你想找到 dups,修改它们,在循环中再次检查,直到所有 dup 现在都是唯一的?

                  OK, there are a lot of examples of duplicate detection and removal in php arrays, using array_unique() etc but what if you want to find dups, modify them, check again in a loop until all dups are now unique?

                  我认为这有点像使用 array_filter()... 所以作为一个更具体的例子,下面是一个类似这样的 sql 语句的结果:

                  I think it's something like using array_filter()... so as a more specific example, here's what would come out of a sql statement something like this:

                  SELECT id, list.comboname 
                  FROM list
                     INNER JOIN (
                        SELECT comboname 
                        FROM list
                         GROUP BY comboname 
                         HAVING count(id) > 1
                     ) dup ON list.comboname = dup.comboname
                  

                  到表中重复的数组:

                  Array ( 
                      [0] => 49 
                      [1] => big.dup  
                      [2] => 233  
                      [3] => another.duplicate  
                      [4] => 653  
                      [5] => big.dup  
                      [6] => 387  
                      [7] => big.dup  
                      [8] => 729  
                      [9] => another.duplicate  
                      [10] => 1022  
                      [11] => big.dup   
                  )
                  

                  现在我想要的是一些 PHP 删除字符直到句号,所以它们是唯一的 [或者如果需要在末尾添加数字]

                  Now what I want is some PHP to delete characters until the period so they are unique [or add numbers if needed to the end]

                  所以结果是:

                  Array (  
                      [0] => 49  
                      [1] => big.dup  
                      [2] => 233  
                      [3] => another.duplicate  
                      [4] => 653  
                      [5] => big.du  
                      [6] => 387  
                      [7] => big.d  
                      [8] => 729  
                      [9] => another.duplicat  
                      [10] => 1022  
                      [11] => big.dup1  
                  )
                  

                  在保留原始值(即 big.dup 和另一个

                  While retaining the original value (i.e. big.dup and another.duplicate)... I've looked through just about every PHP array function trying to imagine a strategy ... ideas?

                  推荐答案

                  对于你问题中的数组,如果重复的话,在末尾添加数字,你只需要循环一次数组并临时构建一个辅助数组存储是否已经找到值(以及多久):

                  For the array in your question and for adding numbers at the end if a duplicate, you only need to loop over the array once and temporary build up a helper array that stores if a value has been already found (and how often):

                  $found = array();
                  
                  foreach($array as &$value)
                  {
                      if (is_int($value)) continue; # skip integer values
                  
                      if (isset($found[$value]))
                      {
                          $value = sprintf('%s-%d', $value, ++$found[$value]);
                      }
                      else
                      {
                          $found[$value] = 0;
                      }
                  }
                  unset($value);
                  

                  演示

                  这篇关于PHP:如何识别和更改数组中的重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:考虑到 Gmail (user.name+label@gmail.com),如何在 PHP 中检查重复的电子邮件地址 下一篇:查找所有具有相同值的数组键

                  相关文章

                  <i id='ITke6'><tr id='ITke6'><dt id='ITke6'><q id='ITke6'><span id='ITke6'><b id='ITke6'><form id='ITke6'><ins id='ITke6'></ins><ul id='ITke6'></ul><sub id='ITke6'></sub></form><legend id='ITke6'></legend><bdo id='ITke6'><pre id='ITke6'><center id='ITke6'></center></pre></bdo></b><th id='ITke6'></th></span></q></dt></tr></i><div id='ITke6'><tfoot id='ITke6'></tfoot><dl id='ITke6'><fieldset id='ITke6'></fieldset></dl></div>

                  <legend id='ITke6'><style id='ITke6'><dir id='ITke6'><q id='ITke6'></q></dir></style></legend>

                  1. <tfoot id='ITke6'></tfoot>
                      <bdo id='ITke6'></bdo><ul id='ITke6'></ul>
                  2. <small id='ITke6'></small><noframes id='ITke6'>