• <bdo id='ma76j'></bdo><ul id='ma76j'></ul>
      1. <small id='ma76j'></small><noframes id='ma76j'>

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

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

        合并两个大小相等的平面索引数组,以便将值以

        时间:2023-10-04

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

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

                  本文介绍了合并两个大小相等的平面索引数组,以便将值以交替方式推入结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有两个数组:

                  $a1 = array(0, 1, 2);
                  $a2 = array(3, 4, 5);
                  

                  我希望能够使用一种合并技术来交替数组值,而不仅仅是连接它们.我想要这个结果:

                  I want to be able to do a merge technique that alternates the array values and not just concatenate them. I want this result:

                  array(0, 3, 1, 4, 2, 5);
                  

                  是否有一种本地方法可以做到这一点,因为这里的性能是一个问题,因为我需要这样做数千次

                  Is there a native way to do this as performance is an issue here since I need to do this thousands of times

                  请注意,我知道我可以这样做:

                  Please note, I know I can do it like this:

                  for (var $i = 0; $i < count($a1); $i++) {
                      newArray[] = $a1[$i];
                      newArray[] = $b1[$i];
                  }
                  

                  如果有更快的方法,我正在寻找一种内置方法.

                  I'm looking for a built in way if there is a faster one.

                  推荐答案

                  $count = count($a1);
                  for ($i = 0; $i < $count; $i++) {
                      $newArray[] = $a1[$i];
                      $newArray[] = $b1[$i];
                  }
                  

                  我在这里的工作已经完成.

                  My work here is done.

                  $a1 = array(0,1,2);
                  $a2 = array(3,4,5);
                  
                  $start = microtime(TRUE);
                  
                  for($t = 0; $t < 100000; $t++)
                  {
                      $newArray = array();
                      $count = count($a1);
                      for ($i = 0; $i < $count; $i++)
                      {
                          $newArray[] = $a1[$i];
                          $newArray[] = $a2[$i];
                      }
                  }
                  echo  round(microtime(TRUE) - $start, 2); # 0.6
                  
                  $a1 = array(0,1,2);
                  $a2 = array(3,4,5);
                  
                  $start = microtime(TRUE);
                  
                  for($t = 0; $t < 100000; $t++)
                  {
                      $newArray = array();
                      for ($i = 0; $i < count($a1); $i++)
                      {
                          $newArray[] = $a1[$i];
                          $newArray[] = $a2[$i];
                      }
                  }
                  echo  round(microtime(TRUE) - $start, 2); # 0.85
                  

                  因此,预先计算的数组大小将是 ~1/4 [需要引用](在 100.000 次迭代中,您总共将获得 0.2)更快.如果将 count() 放入循环中,它将在每次 iteration 上重新计数.1/4 在我看来是相当快的.如果你正在寻找编译函数,你可以停止.

                  So pre-counting array size will be ~1/4 [citation needed] (on freakin' 100.000 iterations you will gain 0.2 in total) faster. If you put count() inside loop, it will recount on every iteration. 1/4 seems to me a reasonably faster. If you are looking for compiled function, you can stop.

                  附言Benchmark 就像比基尼,它向你展示了一切,什么都没有.

                  P.S. Benchmark is like bikini, it shows you everything, and nothing.

                  这篇关于合并两个大小相等的平面索引数组,以便将值以交替方式推入结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:组合/合并日期范围的算法 下一篇:将变量传递给从命令行运行的 PHP 脚本

                  相关文章

                  最新文章

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

                    <legend id='RA5md'><style id='RA5md'><dir id='RA5md'><q id='RA5md'></q></dir></style></legend>
                  1. <small id='RA5md'></small><noframes id='RA5md'>

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