Home  >  Article  >  Backend Development  >  PHP初记(6) - 数组添加元素

PHP初记(6) - 数组添加元素

WBOY
WBOYOriginal
2016-06-13 11:01:35836browse

PHP小记(6) - 数组添加元素

今天学到了PHP数组添加一个元素的新方式。

以前总是用push()函数来添加:

$arr = array();array_push($arr, el1, el2 ... eln);

?

但其实有一种更直接方便的做法:

$arr = array();$arr[] = el1;$arr[] = el2;...$arr[] = eln;

?

而且有实验证明,第二种方法的效率比第一种方法高出将近一倍!

我们来看下面的例子:

$t = microtime(true); $array = array(); for($i = 0; $i '; $t = microtime(true); $array = array(); for($i = 0; $i <p>运行脚本,结果为:</p><p></p><div class="quote_title">?写道<div class="quote_div">Run 1 <br>0.0054171085357666 // array_push <br>0.0028800964355469 // array[] <br>Run 2 <br>0.0054559707641602 // array_push <br>0.002892017364502 // array[] <br>Run 3 <br>0.0055501461029053 // array_push <br>0.0028610229492188 // array[]<p>?</p>
<p>确实长见识了。</p>
<div class="clear">
                 
              
              
        
            </div>
</div>
</div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn