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

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

        查找和替换数组中的重复项

        Find and replace duplicates in Array(查找和替换数组中的重复项)
          <tbody id='FHpwu'></tbody>

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

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

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

                  <tfoot id='FHpwu'></tfoot>
                • 本文介绍了查找和替换数组中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要用一些随机值填充数组,但如果数组中有重复项,我的应用程序将无法正常工作.所以我需要编写脚本代码来查找重复项并将它们替换为其他一些值.好的,例如我有一个数组:

                  I need to make app with will fill array with some random values, but if in array are duplicates my app not working correctly. So I need to write script code which will find duplicates and replace them with some other values. Okay so for example i have an array:

                  <?PHP
                  $charset=array(123,78111,0000,123,900,134,00000,900);
                  
                  function arrayDupFindAndReplace($array){
                  
                  // if in array are duplicated values then -> Replace duplicates with some other numbers which ones I'm able to specify.
                  return $ArrayWithReplacedValues;
                  }
                  ?>
                  

                  因此结果应该是具有替换重复值的相同数组.

                  So result shall be the same array with replaced duplicated values.

                  推荐答案

                  您可以只跟踪到目前为止看到的单词并随时替换.

                  You can just keep track of the words that you've seen so far and replace as you go.

                  // words we've seen so far
                  $words_so_far = array();
                  // for each word, check if we've encountered it so far
                  //    - if not, add it to our list
                  //    - if yes, replace it
                  foreach($charset as $k => $word){
                      if(in_array($word, $words_so_far)){
                          $charset[$k] = $your_replacement_here;
                      }
                      else {
                          $words_so_far[] = $word;
                      }
                  }
                  

                  对于稍微优化的解决方案(对于没有那么多重复项的情况),请使用 array_count_values() (参考这里) 来计算它出现的次数.

                  For a somewhat-optimized solution (for cases where there are not that many duplicates), use array_count_values() (reference here) to count the number of times it shows up.

                  // counts the number of words
                  $word_count = array_count_values($charset);
                  // words we've seen so far
                  $words_so_far = array();
                  // for each word, check if we've encountered it so far
                  //    - if not, add it to our list
                  //    - if yes, replace it
                  foreach($charset as $k => $word){
                      if($word_count[$word] > 1 && in_array($word, $words_so_far)){
                          $charset[$k] = $your_replacement_here;
                      }
                      elseif($word_count[$word] > 1){
                          $words_so_far[] = $word;
                      }
                  }
                  

                  这篇关于查找和替换数组中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

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

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

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

                        <legend id='gQWXq'><style id='gQWXq'><dir id='gQWXq'><q id='gQWXq'></q></dir></style></legend>
                            <tbody id='gQWXq'></tbody>
                          <tfoot id='gQWXq'></tfoot>