• <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:如何识别和更改数组中的重复值?

        PHP: How to identify AND CHANGE duplicate values in an array?(PHP:如何识别和更改数组中的重复值?)
          <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:如何识别和更改数组中的重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='ITke6'></tbody>
                  <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>

                    • <tfoot id='ITke6'></tfoot>
                        <bdo id='ITke6'></bdo><ul id='ITke6'></ul>
                      • <small id='ITke6'></small><noframes id='ITke6'>